'Local Variable 'X' Referenced Before Assignment in Django Views

I am working with Django and trying to create two forms on one page which I am struggling with.

I would like to know why I am getting the error "local variable 'street_address' referenced before assignment" originating from line 131 in views.

VIEWS.PY - CheckoutView Class

def post(self, *args, **kwargs):
    form = AddressForm(self.request.POST or None)
    form_2 = PaymentForm(self.request.POST or None)
    # order, created = Order.objects.get_or_create(customer=customer, complete=False)
    # order = Order.objects.get(user=self.request.user, ordered=False)
    if all([form.is_valid(), form_2.is_valid()]):
        street_address = form.cleaned_data.get('street_address')
        city = form.cleaned_data.get('city')
        postcode = form.cleaned_data.get('postcode')
        country = form.cleaned_data.get('country')
        state = form.cleaned_data.get('state')
        shipping_method = form.cleaned_data.get('shipping_method')
        card_num = form.cleaned_data.get('card_num')
        cvc = form.cleaned_data.get('cvc')
        exp_date = form.cleaned_data.get('exp_date')

    address = Shipping(
        user=self.request.user,
        street_address=street_address,
        city=city,
        postcode=postcode,
        country=country,
        state=state,
        shipping_method=shipping_method
    )

    card_details = Payment(
        user=self.request.user,
        card_num=card_num,
        cvc=cvc,
        exp_date=exp_date
    )

    address.save()
    card_details.save()
    return render(self.request,"Order_Management/checkout.html")'''


Solution 1:[1]

The problem is that you are checking to see if the forms are valid.. and if they are, you fetch the values from them and assign them to variables.

But, the part of your code where you construct your Shipping and Payment models and save them is executed every time, it's not indented within the block where you checked the validity of the forms.

The result is that if either form is invalid, your code will try to construct the models anyway, and it won't be able to because you haven't assigned the variables from the forms.

So you need to indent all that code as well so it's all under your validity check.

if all([form.is_valid(), form_2.is_valid()]):
    street_address = form.cleaned_data.get('street_address')
    city = form.cleaned_data.get('city')
    postcode = form.cleaned_data.get('postcode')
    country = form.cleaned_data.get('country')
    state = form.cleaned_data.get('state')
    shipping_method = form.cleaned_data.get('shipping_method')
    card_num = form.cleaned_data.get('card_num')
    cvc = form.cleaned_data.get('cvc')
    exp_date = form.cleaned_data.get('exp_date')

    address = Shipping(
        user=self.request.user,
        street_address=street_address,
        city=city,
        postcode=postcode,
        country=country,
        state=state,
        shipping_method=shipping_method
    )

    card_details = Payment(
        user=self.request.user,
        card_num=card_num,
        cvc=cvc,
        exp_date=exp_date
    )

    address.save()
    card_details.save()

return render(self.request,"Order_Management/checkout.html")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 little_birdie