views:

51

answers:

2

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 %}
+5  A: 

This will always run:

return render_to_response('edit_client.html', {'form': form}

But if request.method is not POST, nothing is assigned to 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))
leoluk
Well that has made that error message go away but for some reason my edit client forms gives me a clean blank form. I should be getting a form with data stored already which I can then change and save.
Shehzad009
there is no GET handler, of course you see a blank form
leoluk
I did something like this.
Shehzad009
@Shehzad009 - if you did something like this, can you accept this answer, using the checkmark under the vote count to the left of the answer? Welcome to Stack Overflow!
Dominic Rodger
whoops let me refrain my previous statement. Looks like I got a time out as well so I could not edit my previous statement as well. Anyway back on the subject. I there any documentation to enable GET handler so I can see my previous data in the form?
Shehzad009
What's wrong with your previous statement? By the way, you can edit your posts as often as you like.
leoluk
What is wrong is I have not done something like that. (I had Accidentally pressed enter key as I thought I could start a paragraph). I wonder how I add a GET Handler. I initial thought when you said that was to change all the POST to GET but that seems wrong so it must be something else. This is why I ask for some documentation in my previous post.
Shehzad009
You don't need to "add a GET handler". Everything is working exactly as it should. Previously-filled in data will only show up in response to a form submission, or POST request. Otherwise the form will be, and *should* be, blank. Here is some relevant documentation on using forms with Django: http://docs.djangoproject.com/en/dev/topics/forms/
Jordan Reiter
Let me explain again. I already know how to create a form! I have created an add client Form but what I want is a function that allows me to "edit" the client form. And when I click on an edit button I should see the form in its context where I can edit the form. Now I have tried using what I have written up earlier but that has not worked.
Shehzad009
+1  A: 

In your edit_client method, you pass form in the response, however, if the method wasn't a POST, you won't have initialized a form.

kchau