4

I am trying to redirect the user back to the page where the comment was posted. I found this post on Django's site but I am doing something wrong because it won't redirect back.

Where should the input be placed to have it properly redirected?

{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
  {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
  {% for field in form %}
    {% if field.is_hidden %}
      {{ field }}
    {% else %}
      {% if field.errors %}{{ field.errors }}{% endif %}
            <input type="hidden" name="next" value="{% url proposal proposal.id %}" />
      <p
        {% if field.errors %} class="error"{% endif %}
        {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "name" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "email" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "url" %} style="display:none;"{% endifequal %}
         {% ifequal field.name "title" %} style="display:none;"{% endifequal %}>
        <!-- {{ field.label_tag }}  -->{{ field }}
      </p>
    {% endif %}
  {% endfor %}
  <p class="submit">
    <!-- <button><input type="submit" name="post" value="{% trans "Send" %}" /></button> -->
        <button type="submit">Send</button>
    <!-- <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> -->
  </p>
</form>
Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
Emile
  • 3,464
  • 8
  • 46
  • 77

4 Answers4

1

The problem with axel22's answer is that it requires a change to each template that requires the comment form - if you have multiple object types that can be commented on, this is not DRY.

Unfortunately, I'm also still looking for an answer that works.

Community
  • 1
  • 1
datakid
  • 2,293
  • 5
  • 23
  • 35
1

Maybe you don't need to check for next variable in your template. You could try changing:

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

to just:

<input type="hidden" name="next" value="/added/comment/page/" />

In case you use views.py, redirecting from there seems more obvious, at least for me, as it helps keep the concern away from the template:

from django.http import HttpResponseRedirect
HttpResponseRedirect("/path/to/redirect")
Arbie Samong
  • 1,195
  • 1
  • 15
  • 17
  • Arbie, thanks for the comment. I think you are onto it. The one question I have is how/where do I define 'next'? Here is a sample of my comment section for a picture object: gist.github.com/4ea34b6f559a15c91d05 Thanks again for the help! – Emile Mar 07 '11 at 02:51
  • For mine, I just used a function that rendered the same page the comment form is on. – Emile Mar 07 '11 at 21:21
0

See my solution here: Django: Redirect to current article after comment post

It basically uses a view that's triggered by the comment post url which redirects back to the original referrer page.

Community
  • 1
  • 1
Corey
  • 166
  • 3
  • 14
0

if you are using {% render_comment_form for object %} tag in your template, just add something like {% url object's_named_view object.id as next %} or wrap it with {% with object.get_absolute_url as next %} ... {% endwith %} construction.

axel22
  • 32,045
  • 9
  • 125
  • 137
delta32
  • 387
  • 2
  • 7