Django评论从发布/预览重定向

Django评论从发布/预览重定向,django,django-comments,Django,Django Comments,我正在实现django评论应用程序。 单击post而不是post页面时,重定向到当前页面的最佳方式是什么 我遵循了以下指南: 我的表单如下所示: {% load comments i18n %} <form action="{% comment_form_target %}" method="post">{% csrf_token %} {% if next %}<div><input type="hidden" name="next" value="{{

我正在实现django评论应用程序。 单击post而不是post页面时,重定向到当前页面的最佳方式是什么

我遵循了以下指南:

我的表单如下所示:

{% 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>
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( "/" )
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'),
My URL.py看起来像:

{% 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>
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( "/" )
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
我想我已经修改了我的观点。有什么想法吗

我的应用程序叫照片,我的模型叫图像


谢谢

我不明白你为什么需要发表评论。相反,我认为您应该修复下一个领域:

{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
{%if next%}{%endif%}
这里,仅当设置了“next”上下文变量时,才会输出“next”隐藏输入。你的目标应该是:

  • 如果可能的话,让next成为{{next}}
  • 回退到已注释对象的绝对url
它可能看起来像:

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

这假设您的模型有一个正确定义的get\u absolute\u url方法

请注意,我通过阅读以下内容了解form.target_对象:

  • comment templatetag的代码,我注意到它将注释表单与目标对象作为第一个参数实例化,并

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


  • 我更新了原来的帖子。谢谢你从哪里得到的?comment_id,post_id=request.GET['c'].split(':')我更改了那一行,它现在将我带到一个空白页。下一步我该怎么做?还有什么?在我的更新中,我更喜欢form.target_对象而不是form.instance.content_对象。