• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python Django删除当前对象(Python Django delete current object)_电脑培训 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

Python Django删除当前对象(Python Django delete current object)

案例我在/ notes / get / 1 /其中id = 1,并且我在note.html中创建了一个“Delete Note”链接。 我需要它从数据库和应用程序中删除当前的注释并重定向到/ notes / all。 你可以帮我用def delete中的代码吗?

models.py:

class Note(models.Model): 
   title = models.CharField(max_length=200)
   body = models.TextField()
   cr_date = models.DateTimeField(auto_now_add=True)

urls.py:

urlpatterns = patterns('',

  url(r'^all/$', 'note.views.notes'), 
  url(r'^get/(?P<note_id>\d+)/$', 'note.views.note'), #
  url(r'^language/(?P<language>[a-z\-]+)/$', 'note.views.language'), # 
  url(r'^create/$', 'note.views.create'),
  url(r'^delete/$', 'note.views.delete'), <--------------------------
  url(r'^search/$', 'note.views.search_titles'),
 )

views.py:

def delete(??????):
    ?????  <-------------------------------------------
    return HttpResponseRedirect('/notes/all')

Case I am in /notes/get/1/ where id=1 and I have created a "Delete Note" link in note.html. I need it to delete the current note from database and app and redirect to /notes/all. Can you help me with the code in def delete?

models.py:

class Note(models.Model): 
   title = models.CharField(max_length=200)
   body = models.TextField()
   cr_date = models.DateTimeField(auto_now_add=True)

urls.py:

urlpatterns = patterns('',

  url(r'^all/$', 'note.views.notes'), 
  url(r'^get/(?P<note_id>\d+)/$', 'note.views.note'), #
  url(r'^language/(?P<language>[a-z\-]+)/$', 'note.views.language'), # 
  url(r'^create/$', 'note.views.create'),
  url(r'^delete/$', 'note.views.delete'), <--------------------------
  url(r'^search/$', 'note.views.search_titles'),
 )

views.py:

def delete(??????):
    ?????  <-------------------------------------------
    return HttpResponseRedirect('/notes/all')

原文:https://stackoverflow.com/questions/19382664
更新时间:2022-03-21 15:03

最满意答案

from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse


def delete(request, id):
    note = get_object_or_404(Note, pk=id).delete()
    return HttpResponseRedirect(reverse('notes.views.notes'))

并在urls.py

url(r'^delete/(?P<id>\d+)/$','project.app.views.delete'),

确保在删除对象之前检查用户权限,可以使用@permission_required修饰器https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-permission-required-decorator 。 如果您没有检查,用户可以轻松删除所有笔记。

通常,使用POST或DELETE请求而不是GET来从数据库中删除对象是一个好主意。 想象一下,google-bot抓取您的网站并访问notes / delete / 2。


from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse


def delete(request, id):
    note = get_object_or_404(Note, pk=id).delete()
    return HttpResponseRedirect(reverse('notes.views.notes'))

And in urls.py

url(r'^delete/(?P<id>\d+)/$','project.app.views.delete'),

Make sure that you check the user permissions before deleting an object, you can use the @permission_required decorator https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-permission-required-decorator. If you don't check this an user can delete all notes easily.

Usually it's a good idea to remove objects from the DB using a POST or DELETE request, instead of a GET. Imagine that google-bot crawls your site and visits notes/delete/2.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap