我正在尝试在我的项目中使用评论应用程序.
我尝试使用代码({%render_comment_form for event%}),在这里的文档中显示: Django评论
问题是如何在提交后将表单重定向到同一页面.
另外一个大问题是:目前如果我们在for中发现任何错误,那么我们将被重定向到预览模板.是否可以避免此行为并在同一表单上显示错误(在同一页面上)?
我将告诉你我是如何在我的博客中解决它的,所以你可以做类似的事情.我的评论是条目申请中的入门模型.
首先为Entry(like)对象添加新方法.
def get_absolute_url(self):
return "/%i/%i/%i/entry/%i/%s/" % (self.date.year, self.date.month, self.date.day, self.id, self.slug)
Run Code Online (Sandbox Code Playgroud)
它为入口对象生成url.URL示例:/ 2009/12/12/entry/1/lorem-ipsum /
要urls.py添加1行:
(r'^comments/posted/$', 'smenteks_blog.entries.views.comment_posted'),
Run Code Online (Sandbox Code Playgroud)
所以现在你应该在你的urls.py文件中至少有两行注释.
(r'^comments/posted/$', 'smenteks_blog.entries.views.comment_posted'),
(r'^comments/', include('django.contrib.comments.urls')),
Run Code Online (Sandbox Code Playgroud)
对于views.py文件中的条目(如)应用程序添加功能:
from django.contrib.comments import Comment #A
...
def comment_posted(request):
if request.GET['c']:
comment_id = request.GET['c'] #B
comment = Comment.objects.get( pk=comment_id )
entry = Entry.objects.get(id=comment.object_pk) #C
if entry:
return HttpResponseRedirect( entry.get_absolute_url() ) #D
return HttpResponseRedirect( "/" )
Run Code Online (Sandbox Code Playgroud)
现在:
接下来要做的就是覆盖preview.html模板:
| 归档时间: |
|
| 查看次数: |
6797 次 |
| 最近记录: |