1

I am using the Django comments framework in two places on my site. After each submission, I'd like for the user to just be redirected back to the original page they were on.

How do you define the "next" variable so the user is redirected?

Information on the redirect : http://docs.djangoproject.com/en/dev/ref/contrib/comments/#redirecting-after-the-comment-post

Also, here is the form I am using. The comment.types do not work, but that is what I think I am supposed to do - define two different next inputs for each comment type (picture vs meal).

{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
    {% if comment.type == '19' %}
    <input type="hidden" name="next" value="{% url meal comment.object_pk %}" />
    {% endif %}
    {% if comment.type == '23' %}
    <input type="hidden" name="next" value="{% url picture comment.object_pk %}" />
    {% endif %}
  <!-- <input type="hidden" name="next" value="{{ next }}" /> -->
  {% for field in form %}
    {% if field.is_hidden %}
      {{ field }}
    {% else %}
      {% if field.errors %}{{ field.errors }}{% endif %}
      <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 type="submit">Send</button>
    <!-- <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> -->
  </p>
</form>

And then on the Meal & Picture pages I have:

    <h4>Post a Message</h4>
{% render_comment_form for meal %}

    <h4>Post a Message</h4>
{% render_comment_form for picture %}
Emile
  • 3,464
  • 8
  • 46
  • 77
  • what does a 'next' field actually do here? Can you explain more? – tamizhgeek Mar 07 '11 at 19:19
  • In Django comments, there is a next value you can define that will redirect the user after the comment has been submitted. http://docs.djangoproject.com/en/dev/ref/contrib/comments/#redirecting-after-the-comment-post – Emile Mar 07 '11 at 19:43

2 Answers2

2

Figured it out. To use the next with multiple objects, use an if statement.

{% if picture %}
<input type="hidden" name="next" value="{% url picture picture.id %}" />
{% endif %}
Emile
  • 3,464
  • 8
  • 46
  • 77
  • 1
    You should do value="{{ form.target_object.get_absolute_url }}" because it doesn't depend on the model type, and get_absolute_url is a standard method for models. – jpic Apr 02 '12 at 13:36
0

If you want to stay on the same page ajax is an option, you could use something like django_ajaxcomments, there are quite a few posts on others ways to do this with ajax.

JamesO
  • 25,178
  • 4
  • 40
  • 42