Valid form is not coming out as valid in django -


i have form in django valid when try validate before grabbin gthe cleaned_data, giving me error , dont know why happening. can me figure out.. have feeling small getting coders block , can't see it..

here view.py method:

def profile_setup(request):     if 'username' not in request.session:         return redirect('login')     else:         username = request.session['username']         currentuser = user.objects.get(username = username)         if request.method == 'post':             form = profileform(request.post)             print(form)             if form.is_valid():                 cd = form.cleaned_data                 age = cd['age']                 print(age)                 city = cd['city']                 print(city)                 phone = cd['phone']                 print(phone)                 privacy = cd['privacy']                 print(privacy)                 new_profile = profile.objects.create(                     user = currentuser,                     age = age,                     city = city,                     phone = phone,                     privacy = privacy,                 )                 return redirect('accounts')         else:             form = profileform()             message = 'fill out form below'             parameters = {                 'form':form,                 'currentuser':currentuser,                 'message':message,             }         return render(request, 'tabs/profile_setup.html', parameters) 

here html:

{% extends "base.html" %}  {% block content %}   <h1>setup profile: {{ currentuser.username }}</h1>   {% if message %}     {{ message }}   {% endif %}   <form action="." method="post">     {% csrf_token %}     {{ form.as_p }}     <input type="submit" name="submit" value="submit">   </form> {% endblock %} 

here getting browser:

unboundlocalerror @ /setup_profile/ local variable 'parameters' referenced before assignment request method: post request url:    http://127.0.0.1:8000/setup_profile/ django version: 1.8.6 exception type: unboundlocalerror exception value:     local variable 'parameters' referenced before assignment exception location: c:\users\omarjandali\desktop\opentab\opentab\tab\views.py in profile_setup, line 153 python executable:  c:\users\omarjandali\appdata\local\programs\python\python36\python.exe python version: 3.6.1 python path:     ['c:\\users\\omarjandali\\desktop\\opentab\\opentab',  'c:\\users\\omarjandali\\appdata\\local\\programs\\python\\python36\\python36.zip',  'c:\\users\\omarjandali\\appdata\\local\\programs\\python\\python36\\dlls',  'c:\\users\\omarjandali\\appdata\\local\\programs\\python\\python36\\lib',  'c:\\users\\omarjandali\\appdata\\local\\programs\\python\\python36',  'c:\\users\\omarjandali\\appdata\\local\\programs\\python\\python36\\lib\\site-packages'] server time:    wed, 26 jul 2017 05:56:29 +0000 traceback switch copy-and-paste view  c:\users\omarjandali\appdata\local\programs\python\python36\lib\site-packages\django\core\handlers\base.py in get_response                                 response = wrapped_callback(request, *callback_args, **callback_kwargs) ... ▶ local vars c:\users\omarjandali\desktop\opentab\opentab\tab\views.py in profile_setup                     return render(request, 'tabs/profile_setup.html', parameters) ... ▶ local vars request information  no data post variable    value csrfmiddlewaretoken  'eppxclvn9jwbfqgqv3lhwnoic53g0ny4' age  '22' city     'riverside' phone    '232414' privacy  '1' submit   'submit' 

everything passing , valid..

it printing form first print statement, nothing after means there error is_valid part, can't figure out...

updated

i figured out giving me error. choices section had models.py file. did fix did not set choices or options in teh models.py file, rather added options in forms.py file , added form rather model... here code looks now...

models.py file

class profile(models.model):     user = models.foreignkey(user, on_delete=models.cascade)  # server     age = models.integerfield(default=0)     city = models.charfield(max_length=45)  # user     phone = models.bigintegerfield(default=0)  # user     privacy = models.smallintegerfield(default=1)  # user     created = models.datetimefield(auto_now_add=true)  # server 

forms.py file:

class profileform(forms.modelform):     split_choices = (('1', 'public'),                      ('2', 'private'))     privacy = forms.typedchoicefield(         choices=split_choices, widget=forms.radioselect, coerce=int     )     class meta:         model = profile         fields = ['age', 'city', 'phone', 'privacy'] 

the views.py file:

def profile_setup(request):     if 'username' not in request.session:         return redirect('login')     else:         username = request.session['username']         currentuser = user.objects.get(username = username)         if request.method == 'post':             form = profileform(request.post)             print(form)             if form.is_valid():                 cd = form.cleaned_data                 age = cd['age']                 print(age)                 city = cd['city']                 print(city)                 phone = cd['phone']                 print(phone)                 privacy = cd['privacy']                 print(privacy)                 new_profile = profile.objects.create(                     user = currentuser,                     age = age,                     city = city,                     phone = phone,                     privacy = privacy,                 )                 return redirect('accounts')         else:             form = profileform()             message = 'fill out form below'             parameters = {                 'form':form,                 'currentuser':currentuser,                 'message':message,             }             return render(request, 'tabs/profile_setup.html', parameters) 

you should move parameters variable out of else clause,

def profile_setup(request):     if 'username' not in request.session:         return redirect('login')     else:         username = request.session['username']         currentuser = user.objects.get(username = username)         message = none         if request.method == 'post':             form = profileform(request.post)             ......         else:             form = profileform()             message = 'fill out form below'         #<==         parameters = {             'form':form,             'currentuser':currentuser,             'message':message,         }     return render(request, 'tabs/profile_setup.html', parameters) 

when request method post, else clause not executed, ie, error raised.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -