Ich habe eine Form in Django, die gültig ist, aber wenn ich versuche, es vor grabbin gthe cleaned_data zu validieren, gibt es mir einen Fehler und ich weiß nicht, warum es passiert. Kann ich es jemand helfen herauszufinden, .. Ich habe das Gefühl ein es etwas klein, aber ich bin immer Programmierer blockieren und es nicht sehen kann ..Gültige Form nicht als gültig in Django
hier ist view.py Methode:
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)
Hier der hTML-Code ist:
{% extends "base.html" %}
{% block content %}
<h1>Setup you 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 %}
Hier ist das, was ich aus dem Browser bin immer:
UnboundLocalError at /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 to 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
GET
No GET data
POST
Variable Value
csrfmiddlewaretoken
'EpPxClvN9jWbFQGqV3lhWnoIC53g0ny4'
age
'22'
city
'riverside'
phone
'232414'
privacy
'1'
submit
'submit'
Alles ist vorbei und ist gültig ..
Es ist das Formular ausdrucken, die die erste Print-Anweisung ist, aber nichts nach, was bedeutet, dass es ein Fehler mit dem is_valid Teil, aber ich kann es nicht herausgefunden ...
AKTUALISIERT
Ich fand heraus, was mir den Fehler gab. Es war der Auswahlbereich, den ich mit der Datei models.py hatte. Was ich getan habe, um es zu beheben, war, dass ich die Optionen oder Optionen in der Datei models.py nicht gesetzt habe, sondern die Optionen in der Datei forms.py hinzugefügt habe und sie dem Formular hinzugefügt wurde, anstatt dem Modell ... hier ist was mein Code sieht aus wie jetzt ...
models.py Datei
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 Datei:
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']
Die views.py Datei:
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)
Das ist nicht das Problem, das ich habe. Das Problem ist, dass das Formular nicht gültig ist .... können Sie auf den aktualisierten Abschnitt des Posts schauen, habe ich etwas gefunden, das ich wissen wollte, ob es dort ein Problem gibt und wie ich dieses Problem beheben kann. @zaidfazil –
Ich dachte, es macht nichts aus –