I'm trying to create a page where I can delete an article using class based views. There is one problem, I am getting an error which says :
id() takes exactly one argument (0 given)
here is the code :
views.py
class DeleteView(View):
def post(self, request, *args, **kwargs):
article = get_object_or_404(Article, id=id)
article.delete()
return HttpResponseRedirect('/')
def get(self, request, *args, **kwargs):
article = Article.objects.get(id=kwargs['id'])
context = {"article": article}
return render_to_response("delete.html", context, context_instance=RequestContext(request))
template.html
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8">
<form action="{% url 'DeleteView' article.id %}" method="POST">
{% csrf_token %}
<input type="hidden" value="{{ article.views.id }}">
<input type="submit" value="Delete">
</form>
</div>
urls.py
url(r'^delete/(?P<id>\d+)/$', DeleteView.as_view(), name="DeleteView"),
The faults are problably on : the views : line 3 | the template : line 2
I am missing something but I couldn't figure it out. How can I get through this problem?
The problem is with your
post
method ofDeleteView
, there you are passingid
which is a python built-in function rather you need to get theid
fromkwargs
:Problem:
Solution: