Reputation: 343
Hi I am trying to update a row in django but I am getting the error
update_form() takes exactly 2 arguments (1 given)
Here is my code:
view.py:
def update_form(request, id):
if request.method == 'POST':
a=newleave.objects.get(id=id)
form = leave_application(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('successful.html')
else:
a=newleave.objects.get(id=id)
form = leave_application(instance=a)
return render_to_response('update_form.html'{'form':form},
context_instance=RequestContext(request))
form.py:
class leave_application(forms.ModelForm):
class Meta:
model = newleave
fields =('First_Name', 'Last_Name', 'department', 'position', 'leave_type', 'Specify_details', 'start_Date', 'end_date', 'total_working_days', 'username')
update_form.html:
<form action ="/update_form/" method="post">{%csrf_token%}
<table>
{{form.as_table}}
</table>
<br>
<input type="submit" name="submit" value="Save Record" >
Can someone tell me what I am doing wrong?
Upvotes: 1
Views: 318
Reputation: 309099
The problem is probably in your urls.py.
The update_form
view takes an argument id
, so you should include it in the url pattern.
url(r'^update_form/(?P<id>\d+)/$', update_form, name='update_form')
For example, to edit the object with id=1, you would go to /update_form/1/
You would have to include the id in the template context
return render_to_response('update_form.html'{'form':form, 'id': id},
context_instance=RequestContext(request))
And include it it the form action:
<form action ="/update_form/{{ id }}/" method="post">{%csrf_token%}
Using the url tag would be slightly better:
<form action ="{% url 'update_form' %}" method="post">{%csrf_token%}
Upvotes: 1
Reputation: 45585
You should pass the id
of the row to the update_form
view:
<form action="{% url 'update_form' id %}" method="post">
And in urls.py:
url(r'^update_form/(?P<id>\d+)/$', update_form, name='update_form'),
Upvotes: 1