Django - Class based views is returning a blank page

1k views Asked by At

I'm trying to use the class based views to create a page where I can delete an article by his ID.

here is the codes to create the pages where I can delete my articles.

views.py

class DeleteView(View):
    def post(self, request, *args, **kwargs):
        article = get_object_or_404(id=request.POST['article_id'])
        article.delete()
        return HttpResponseRedirect('/')

template.html

<form action="views.article.DeleteView" method="POST">
    {% csrf_token %}
    <p>{{ article.views.id }}"</p>
    <input type="submit" value="Delete">
</form>

urls.py

url(r'^delete/(?P<id>\d+)/$', DeleteView.as_view(), name="DeleteView"),

Here is my problem > the page is not rendering, I'm getting a blank page without anything on it, how can I fix this?

******* EDIT *******

views.py

class DeleteView(View):
    def post(self, request, *args, **kwargs):
        article = get_object_or_404(id=request.POST['article_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("template.html", context, context_instance=RequestContext(request)) 
1

There are 1 answers

0
i.krivosheev On BEST ANSWER

Edit in template file:

<form action="{% url "DeleteView" %}" method="POST">
    {% csrf_token %}
    <input type="hidden" name="acticle_id" value="{{ article.id }}">
    <input type="submit" value="Delete">
</form>

Pleas, read this. The template file should end with html, not py.