喂?我试图重定向一个特定的用户到一个自定义的页面,一旦他们在django登录。管理员将被引导到他们通常的管理界面,而这个特定的用户将转到他们自己的自定义页面。
def custLogin(request):
if request.user.username == '***':
return HttpResponseRedirect('http://********************.html')
else:
return login(request,template_name='login.html')
我把urls.py中的accountlogin网址指向custLogin,如下所示。
(r'^accounts/login/', custLogin),
然而,我一直收到错误信息
Caught NoReverseMatch while rendering: Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found.
请大家指点一下?
也许,你应该使用 转向 快捷方式,返回一个 HttpResponseRedirect
,让你的用户到不同的地方后 custLogin
视图处理。
from django.shortcuts import redirect
def custLogin(request):
if request.user.is_staff:
return redirect('/some/admin/url')
else:
return redirect('/some/regular/user/url')
正如文档中所说,你可以使用 redirect()
函数有多种方式:通过传递视图名称、对象或url。参见 文件 更多信息
注意,我使用的是 is_staff
用户字段来确定:用户是否是管理员。
在您的模板中,您可能忘记了在模板的开头做这个。
{% load from future url %}
这是很有用的,当使用。
{% url 'name_of_my_url_inurl.py' %}