0

我正在实现 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,有什么想法吗?

我的应用程序称为照片,我的模型称为图像。

谢谢

4

1 回答 1

1

我不明白你为什么需要你的 comment_posted 视图。相反,我认为你应该修复你的下一个领域:

{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}

在这里,只有设置了“下一个”上下文变量时,才会输出“下一个”隐藏输入。你应该瞄准的是:

  • 如果可能的话,下一个是 {{ next }}
  • 回退到被评论对象的绝对 url

它可能看起来像:

<input type="hidden" name="next" value="{% if next %}{{ next }}{% else %}{{ form.target_object.get_absolute_url }}{% endif %}" />

这假设您的模型具有正确定义的 get_absolute_url 方法。

请注意,我通过阅读了解了 form.target_object:

  1. 注释模板标签的代码,我注意到它以目标对象作为第一个参数来实例化注释表单,并且

  2. 我注意到的注释表单的代码将传递的目标对象存储在 target_object 属性中,使其在 {{ form }} 所在的任何地方都可用

于 2012-04-02T12:00:06.017 回答