picomon
picomon

Reputation: 1519

Django Comments-Redirecting

how can I get rid of users being directed to the “Thanks you for your comment” page after commenting in Django site? I want users to be redirected to the same page they commented. I’m using Django comments.

I’ve tried adding:

         <input type=”hidden” name=”next” value=”"{% url
          django.contrib.comments.views.comments.comment_done %}" />

But it’s not working. Below is codes in my comment/form.html

         {% load comments %}
        {% get_comment_count for sol as comment_count %}
        {% get_comment_list for sol as comment_list %}
         {% get_comment_form for sol as form %}
         {% if user.is_authenticated %}
        <form action="{% comment_form_target %}" method="post">
    {% csrf_token %}
    {% if next %}<input type="hidden" name="next" value="{% url
        django.contrib.comments.views.comments.comment_done %}" />{% endif %}
    {% for field in form %}
        {% if field.is_hidden %}
            {{ field }}
        {% else %}
            {% if field.name != "name" and field.name != "email"
                 and field.name != "url" %}
                {% if field.errors %}{{ field.errors }}{% endif %}
                {{ field }}
            {% endif %}
        {% endif %}
    {% endfor %}
    <input class="submit-post" name="post" type="submit" value="Comment" />

   </form>
     {% else %}
      I'm sorry, but you must be <a href="javascript:alert('send to
     login page')">logged in</a> to submit comments.
      {% endif %} 

Upvotes: 2

Views: 1455

Answers (1)

jpic
jpic

Reputation: 33420

First let's review your code:

<input type=”hidden” name=”next” value=”"{% url
      django.contrib.comments.views.comments.comment_done %}" />
  1. Two double quotes: value=”"{% url

  2. The url is comment_done: so this will redirect to the "Thank you for your comment page", which you want to avoid

  3. Use url names instead of module name: {% url comments-comment-done %} rather than {% url django.contrib.comments.views.comments.comment_done %}

Instead, you can redirect the comment poster to the absolute url of the object he commented:

<input type="hidden" name="next" value="{{ form.instance.content_object.get_absolute_url }}" />

This assume that your model has the standard get_absolute_url() method defined.

Or even, you can redirect the user to the very same page he's on:

<input type="hidden" name="next" value="{{ request.path }}" />

Or the previous page he visited:

<input type="hidden" name="next" value="{{ request.META.HTTP_REFERER }}" />

Upvotes: 5

Related Questions