python - Require help in Django 'local variable 'form' referenced before assignment' -
i having problem in django. have created form in app can take details of client. want create form can allow me edit form. having problems when go /index/edit_client/1, error.
local variable 'form' referenced before assignment
i not know reason why have got error, have looked at, not matters unless of course there way how create edit form edit clients form. here output can 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 %}
this run:
return render_to_response('edit_client.html', {'form': form}
but if request.method
not post
, nothing assigned form
.
fixed code:
@login_required def edit_client(request, id=1): clients_list = client.objects.filter(pk=id) form = clientform() if request.method == 'post': form = clientform(request.post or none) if form.is_valid(): form.save() return httpresponseredirect('/index/clients/') return render_to_response('edit_client.html', {'form': form}, context_instance=requestcontext(request))
Comments
Post a Comment