Hello, I am having problem in django. I have created a form in my app where I can take details of a client. Now I want to create a form which can allow me to edit a form. However I am having some problems when I go to /index/edit_client/1, I get this error.
local variable 'form' referenced before assignment
I do not know what the reason why I have got this error, but from what I have looked at, it does not help matters unless of course there is another way how to create an edit form to edit the clients form. Here are some output that can be helpful too.
# urls.py
urlpatterns = patterns('',
(r'^index/$', login_required(direct_to_template), { 'template': 'index.html' }),
(r'^index/clients/$', client_info),
(r'^index/clients_details/(?P<id>\d+)/$', clients_details),
(r'^index/edit_client/(?P<id>\d+)/$', edit_client),
)
# views.py
@login_required
def edit_client(request, id=1):
clients_list = Client.objects.filter(pk=id)
if request.method == 'POST':
form = ClientForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect('/index/clients/')
else: form = ClientForm()
return render_to_response('edit_client.html', {'form': form}, context_instance=RequestContext(request))
#edit_client.html
{% extends "base.html" %}
{% block content %}
<font face="verdana,news gothic,arial,heltevica,serif">
<h3>Edit Client</h3>
</font>
<form method= "POST" action="">
<font face="verdana,news gothic,arial,heltevica,serif">
<div id="form">
<table>
{{form.as_table}}
</table>
<div align="center" STYLE=" margin-right:190px">
<input type="submit" value="Submit" STYLE="background-color:#E8E8E8; color:#181818 "/>
</div>
</div>
</form>
{% endblock %}