python - Django - How to delete an article? -


i'm trying create button on framework django let me delete own article on blog. tried create code functionality, doesn't work.

views.py

if(request.get.get('delete')):  #if button clicked     delete_article = get_object_or_404(article, id=id)      if request.post:         form = deletenewform(request.post, request.files)          if form.is_valid():              delete_article.delete()             return httpresponseredirect("/") 

template.html

<form enctype='multipart/form-data' action="." method="post" class="form"  autocomplete="off" autocorrect="off">     {% csrf_token %}     <div class="form-group">titre       {{ form.title.errors }}       {{ form.title }}     </div>     <div class="form-group">contenu       {{ form.media }}       {{ form.contenu.errors }}       {{ form.contenu }}     </div>     <div class="form-group">       {{ form.image.errors }}       {{ form.image }}    </div>    <input type="submit" class="btn btn-default" value="edit article"/>    <input type="submit" class="btn btn-default" value="delete article" name="delete"> </form> 

here happen when submit form : not redirected on index should redirect me , article has not been deleted.

is there problem don't let me delete article in these lines?

i have no idea you're doing in views or in template. if want delete something, define separate view that.

# views.py  django.views.generic.base import view  class deleteview(view):     def post(self, request *args, **kwargs):         article = get_object_or_404(id=request.post['article_id'])         # perform validation,          # can user delete article or not, etc.         # if validation successful, delete article         article.delete()         return httpresponseredirect('/') 

your template should this:

<form action="/url/to/delete/view/" method="post">     {% csrf_token %}     <input type="hidden" value="{{ article.id }}">     <input type="submit" value="delete"> </form> 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -