0

Im implementing the django comments app. 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/

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:

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:

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?

My app is called photo, my model is called Image.

Thanks

Joseph Roberts
  • 569
  • 3
  • 10
  • 29

1 Answers1

1

I don't see why you need your comment_posted view. 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
  • fallback on the commented object's absolute 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.

Note that I figured about form.target_object by reading:

  1. the code for the comment templatetag, which i noticed instanciate the comment form with the target object as first argument and

  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

jpic
  • 32,891
  • 5
  • 112
  • 113