有很多原因需要控制用户访问站点的某部分。
一个简单原始的限制方法是检查 request.user.is_authenticated() ,然后重定向到登陆页面:
from django.http import HttpResponseRedirect
def my_view(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login/?next=%s' % request.path)
# ...
或者显示一个出错信息:
def my_view(request):
if not request.user.is_authenticated():
return render_to_response('myapp/login_error.html')
# ...
作为一个快捷方式, 你可以使用便捷的 login_required 修饰符:
SHOPEX仿M18紫色版 ,适合综合商城,服饰商城.化妆品商城等使用.程序基于SHOPEX4.8.5 最新版制作. 安装方法:1.解压上传程序至网站根目录.. 访问:域名/bak.(用户名:admin 密码:123456)2.进入帝国备份王后,配置数据库信息.选择-www.taomoban.net目录.还原数据库.3.修改FTP目录下的config/config.php 数据库连接信息.4.登陆
0
from django.contrib.auth.decorators import login_required @login_required def my_view(request): # ...
login_required 做下面的事情:
如果用户没有登录, 重定向到 /accounts/login/ , 把当前绝对URL作为 next 在查询字符串中传递过去, 例如: /accounts/login/?next=/polls/3/ 。
如果用户已经登录, 正常地执行视图函数。 视图代码就可以假定用户已经登录了。
=
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号