简体   繁体   English

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

[英]Django comments redirect from posted/preview

Im implementing the django comments app. 我正在实施django评论应用。 What is the best way to redirect to the current page when post is clicked rather than the post page? 单击帖子而不是帖子页面时,重定向到当前页面的最佳方法是什么?

I've followed this guide: http://www.omh.cc/blog/2008/mar/9/free-comments-redirection/ 我遵循了该指南: http : //www.omh.cc/blog/2008/mar/9/free-comments-redirection/

My form looks like: 我的表格如下:

{% 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>

My views.py looks like: 我的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( "/" )

My urls.py looks like: 我的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'),

Traceback: 追溯:

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

I think i have modified my views.py incorrectly, any ideas? 我认为我修改了views.py错误,有什么想法吗?

My app is called photo, my model is called Image. 我的应用程序称为照片,我的模型称为图像。

Thanks 谢谢

I don't see why you need your comment_posted view. 我不明白为什么需要您的comment_posted视图。 Instead, i think you should fix your next field: 相反,我认为您应该修复下一个字段:

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

Here, the "next" hidden input is only outputed if a "next" context variable is set. 在此,仅当设置了“下一个”上下文变量时才输出“下一个”隐藏输入。 What you should aim for is: 您应该瞄准的是:

  • have next to be {{ next }} if possible 如果可能,将下一个设为{{next}}
  • fallback on the commented object's absolute url 后退评论对象的绝对URL

It could look like: 它可能看起来像:

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

This assume that your model have a properly defined get_absolute_url method. 假设您的模型具有正确定义的get_absolute_url方法。

Note that I figured about form.target_object by reading: 请注意,我通过阅读了解了form.target_object:

  1. the code for the comment templatetag, which i noticed instanciate the comment form with the target object as first argument and 我注意到注释templatetag的代码实例化了以目标对象作为第一个参数的注释表单,并且

  2. the code for the comment form which i noticed stores the passed target object in the target_object attribute, making it available wherever {{ form }} is 我注意到的注释表单的代码将传递的目标对象存储在target_object属性中,从而使{{form}}所在的位置都可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM