-2
我想爲admin.html頁面中列出的每個帖子創建一個刪除鏈接。django - 在視圖中從數據庫中刪除數據
這裏是我的views.py [只是管理觀看部分]:
@login_required(login_url='/panel/')
def adminView(request):
if request.session['loggedin']=="djangoo":
draft_list = Post.objects.filter(owner=request.user).filter(isdraft=True).order_by("-posted")
p_draft = Paginator(draft_list,15)
publish_list = Post.objects.filter(owner=request.user).filter(isdraft=False).order_by("-posted")
p_publish = Paginator(publish_list,15)
page = request.GET.get('page')
try:
post_d = p_draft.page(page)
post_p = p_publish.page(page)
except PageNotAnInteger:
post_d = p_draft.page(1)
post_p = p_publish.page(1)
except EmptyPage:
post_d = p_draft.page(p_draft.num_pages)
post_p = p_publish.page(p_publish.num_pages)
return render_to_response('admin.html',
{'draft_list':draft_list,'publish_list':publish_list,'post_d':post_d,
'post_p':post_p},
context_instance=RequestContext(request))
else:
HttpResponseRedirect('/panel/')
我urls.py:
urlpatterns = patterns('blog.views',
url(r'^$','index', name='index'),
url(r'^post/(?P<postslug>[-\w]+)',view='singlePost', name='view_blog_post'),
url(r'^panel/$', view='loguserin'),
url(r'^admin/$', view='adminView'),
url(r'^admin/loggedout/$', view='logout', name='logout'),
url(r'^admin/addpost/$', view='addpost', name='addpost'),
)
models.py:
class Post(models.Model):
owner = models.ForeignKey(User)
title = models.CharField(max_length = 100)
body = models.TextField()
slug = AutoSlugField(populate_from='title',unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField(default=False)
def __unicode__(self):
return self.title
@permalink
def get_absolute_url(self):
return ('view_blog_post',None, {'postslug':self.slug})
這裏我的模板文件:
{% for post in post_d %}
{{ post.title }}
<a href="#" >DELETE</a> # ?????
<a href="{% url index %}post/{{ post.slug }}">VIEW</a>
{% endfor %}
我應該如何在我的模板文件中使用它。在一種觀點下有很多觀點。我有點困惑。
謝謝。
這段代碼中沒有顯得有什麼關係刪除。如果您需要您的用戶能夠刪除對象,則需要編寫一個視圖來執行此操作。 –