在 Django 站点中发表评论后,如何摆脱用户被定向到“感谢您的评论”页面的情况?我希望用户被重定向到他们评论的同一页面。我正在使用 Django 注释。
我试过添加:
<input type=”hidden” name=”next” value=”"{% url
django.contrib.comments.views.comments.comment_done %}" />
Run Code Online (Sandbox Code Playgroud)
但它不起作用。下面是我的 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 %}
Run Code Online (Sandbox Code Playgroud)
首先让我们检查一下您的代码:
<input type=”hidden” name=”next” value=”"{% url
django.contrib.comments.views.comments.comment_done %}" />
Run Code Online (Sandbox Code Playgroud)
两个双引号:value= ”” {% url
网址是 comment_done:所以这将重定向到“感谢您的评论页面”,这是您想要避免的
使用 url 名称而不是模块名称:{% url comments-comment-done %}
而不是{% url django.contrib.comments.views.comments.comment_done %}
相反,您可以将评论发布者重定向到他评论的对象的绝对网址:
<input type="hidden" name="next" value="{{ form.instance.content_object.get_absolute_url }}" />
Run Code Online (Sandbox Code Playgroud)
这假设您的模型定义了标准的 get_absolute_url() 方法。
甚至,您可以将用户重定向到他所在的同一页面:
<input type="hidden" name="next" value="{{ request.path }}" />
Run Code Online (Sandbox Code Playgroud)
或者他访问的上一页:
<input type="hidden" name="next" value="{{ request.META.HTTP_REFERER }}" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1401 次 |
最近记录: |