我正在尝试用django写一个基本的网站。我需要用户(我仍然使用默认的)能够登录,注销,注册等。我有基本的功能设置,但我希望用户能够访问一个配置文件页面,在那里它将显示有关用户的基本信息。当然,个人资料页面必须使用login_required装饰器,但我现在设置的方式是,任何人一旦登录,就可以看到任何个人资料页面。
URL文件的一部分:
url(r'^profile/(?P<username>[\w.@+-]+)/$',
login_required(ProfilePageView.as_view())),
如您所见,url应该由"profile/“后跟用户的用户名组成。我如何设置它,以便只有用户名跟在url的"profile/“部分之后的用户才能看到该页面。使用此代码,某些用户可以使用任何用户名登录,然后只需更改url并重新发送get请求。
发布于 2019-02-22 04:30:55
你可以做这样的事情。如果已为每个用户创建了配置文件模型,则还可以使用profile_info=request.user.profile访问配置文件信息
def login_user(request):
username=request.POST.get('username','')
password=request.POST.get('password','')
user=auth.authenticate(username=username,password=password)
if user is not None:
auth.login(request,user)
return HttpResponseRedirect('/loggin/')
else:
return HttpResponseRedirect('/invalid/')
def loggin(request):
username=None
if request.user.is_authenticated:
#profile_info=request.user.profile
info=request.user
return render(request,'base.html',locals())
<head>
<body>
<h5>{{info.first_name}}{{info.first_name}}</h5>
</body>
</head>
https://stackoverflow.com/questions/54813771
复制相似问题