Reputation: 4038
I am trying to create a simple form where user can insert information. If user click send button, information will be displayed.
Here is the code in views.py.
from kay.utils import render_to_response
from myapp.form import ContactForm
from myapp.models import NhanThu
# Create your views here.
@login_required
def index(request):
form_test = ContactForm()
if request.method =="POST" and form_test.validate(request.form):
NhanthuModelbien = NhanThu(subject=ContactForm['subject'])
NhanthuModelbien.put()
return redirect(url_for('myapp/index'))
query = NhanthuModelbien.all().order('-created')
inthuwhat = query.fetch(20)
return render_to_response('myapp/index.html',
{'form2': form_test.as_widget(),'inthura': inthuwhat})
I always receive this errorr "UnboundLocalError: local variable 'NhanthuModelbien' referenced before assignment". Can anybody point out what is wrong here?
Upvotes: 2
Views: 3339
Reputation: 10368
If the condition request.method =="POST" and form_test.validate(request.form)
is false then the variable doesn't get initialized. Then you try to use it when initializing query query = NhanthuModelbien.all().order('-created')
.
Just declare it and initialize it to a default value before the if
.
Upvotes: 4