Saturday, 22 June 2019

UnboundLocalError at /contact_us/ local variable 'form' referenced before assignment


You defined the form variable in this if request.method == 'POST': block. If you access the view with a GET request form gets not defined. 

Cause:
def contact_us(request):
   
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():

Resolution:
def contact_us(request):
    form = ContactForm(request.POST)
    if request.method == 'POST':
 
        if form.is_valid():


No comments:

Post a comment

Note: only a member of this blog may post a comment.

TypeError: argument of type 'WindowsPath' is not iterable

 Please make to modify settings.py file: DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.sqlite3&...