I was wondering if you guys could help. I’m trying to do a simple view where it sends the user to the client creation form, but I keep getting this error:
local variable ‘form’ referenced before assignment
Looking at my code, I can’t see whats wrong.
def add_client(request):
user = request.user
if request.method =='POST':
form = AddClientForm(request.POST)
if form.is_valid():
client = form.save(commit=False)
client.save()
return HttpResponseRedirect('/')
else:
form = AddClientForm()
return render_to_response('clients/addClient.html', { 'form': form, 'user': user, }, context_instance=RequestContext(request))
Anyone tell me where I went wrong?
This is what is happening:
ifblock is not being entered.formvariable is not defined.formvariable in thereturnstatement.As to how to fix it, that’s really for you to decide. What the fix is depends on what you want your code to do in case the request method is not
POST.