我正在实施django评论应用。 单击帖子而不是帖子页面时,重定向到当前页面的最佳方法是什么?
我已遵循本指南:http://www.omh.cc/blog/2008/mar/9/free-comments-redirection/
我的表单如下:
{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
{% for field in form %}
{% if field.is_hidden %}
<div>{{ field }}</div>
{% else %}
{% if field.name != "email" and field.name != "url" %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{{ field.label_tag }} {{ field }}
</p>
{% endif %}
{% endif %}
{% endfor %}
<p class="submit"><input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" /></p>
</form>
我的views.py看起来像:
def comment_posted( request ):
if request.GET['c']:
comment_id, post_id = request.GET['c'].split( ':' )
post = Image.objects.get( pk=post_id )
if post:
return HttpResponseRedirect( post.get_absolute_url() )
return HttpResponseRedirect( "/" )
我的urls.py看起来像:
urlpatterns = patterns('',
url(r'^other/', include('other.urls')),
url(r'^live/', include('live.urls')),
url(r'^photo/', include('photo.urls')),
url(r'^comments/posted/$', 'photo.views.comment_posted'),
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^search/', SearchView(template=None, searchqueryset=None, form_class=SearchForm), name='haystack_search'),
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/comments/posted/?c=10
Django Version: 1.3.1
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'other',
'live',
'photo',
'haystack',
'django.contrib.flatpages',
'django.contrib.comments',
'django.contrib.admin',
'django.contrib.admindocs']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')
Traceback:
File "/export/mailgrp4_a/sc10jbr/lib/python/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/cserv2_a/soc_ug/sc10jbr/WWWdev/dbe/photo/views.py" in comment_posted
17. comment_id, post_id = request.GET['c'].split( ':' )
Exception Type: ValueError at /comments/posted/
Exception Value: need more than 1 value to unpack
我想我已经修改了我的views.py错误,任何想法?
我的应用程序称为照片,我的模型称为图像。
由于
答案 0 :(得分:1)
我不明白为什么你需要你的comment_posted视图。相反,我认为你应该修复你的下一个领域:
{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
这里,如果设置了“下一个”上下文变量,则“下一个”隐藏输入仅被输出。你应该瞄准的是:
它可能看起来像:
<input type="hidden" name="next" value="{% if next %}{{ next }}{% else %}{{ form.target_object.get_absolute_url }}{% endif %}" />
这假设您的模型具有正确定义的get_absolute_url方法。
请注意,我通过阅读
来了解form.target_object评论模板标签的代码,我注意到将评论表格与目标对象作为第一个参数进行实例化并且
我注意到的评论表单的代码将传递的目标对象存储在target_object属性中,使其在{{form}}