我写了一个留言板,当用户留言并成功返回页面时
我想提醒(“成功留言,非常感谢。”)
我发现一种方法是使用return redirect(reverse(...))
但是当我尝试的时候出现了一个错误:
Reverse for '/maininfo/#5thPage' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
请帮帮我,谢谢。
views.py
def maininfo(request):
return render(request, 'zh_tw/maininfo.html',)
def create_post(request):
if request.method == 'POST':
form = MessageForm(request.POST)
if form.is_valid():
form.save()
# return HttpResponseRedirect('/maininfo/#5thPage')
return redirect(reverse("/maininfo/#5thPage"), {"alert":'sucess leaving message,Thank you very much.'})
return render(request, "zh_tw/maininfo.html",{'form': form,'anchor':'#5thPage'})urls.py
urlpatterns = patterns('',url(r'^maininfo/$', views.maininfo, name='maininfo'),)
模板: zh_tw/contact.html
(这是zh_tw/maininfo.html包含的锚页)
<script type="text/javascript">
$( document ).ready(function() {
{% if alert %}
alert('{{alert}}');
{% endif %}
});
</script>发布于 2014-11-28 12:41:43
reverse的参数是urlpattern中的名称或视图模块。在您的情况下,它应该是
views.maininfo或
'maininfo'此外,$表示Python正则表达式中字符串的结尾。因此,您可能无法使用模式^maininfo/$解析'/maininfo/#5thPage‘。在这里找到更多信息:https://docs.djangoproject.com/en/dev/topics/http/urls/。
https://stackoverflow.com/questions/27180528
复制相似问题