这似乎是一个非常简单的问题,但我无法让它工作。?next=.....如果@login_required装饰器出现在特定的视图函数中,django-registration 将正确传递。一旦用户登录,用户将被重定向到他想查看的页面。这很好并且工作正常。
我的问题是我有这个不需要登录的 html 页面,但是如果你想登录,我有一个<a href='{% url auth_login>礼物。如果用户从此 html 登录,则?next=显示空白并重定向到默认位置accounts/profile/。我还没有设置用户配置文件,所以我收到TemplateDoesNotExist错误消息。
我有这样的事情:
views.py
...
def display_all(request):
response = list_detail.object_list(
request,
queryset = MyModel.objects.all(),
template_name = 'show_all.html'
)
return response
-
show_all.html
<html>
<head>
<title> Display Everything </title>
</head>
<body>
<a href='{% url auth_login %}?next={{ request.path }}'>Login</a>
<h1> Displaying all items! </h1>
<ul>
{% for item in object_list %}
<li> {{ item.name }} </li>
{% endfor %}
</ul>
</body>
</html>
在这里,我通过列出所有内容来显示所有内容,如果您想登录,我在顶部有一个登录链接。{{ request.path }}返回一个空字符串,所以我得到的最终结果http://localhost:8000/accounts/login/?next=将重定向到 default accounts/profile/。