开发者

Django: How to define "Next" within the Comments form

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.t开发者_开发问答ype == '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 %}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜