我可以使用用户在 login.html 中获取用户配置文件/img 吗?我想在我的所有页面中加载登录模块。包含模板的最佳做法是什么?
模型
class UserProfile(models.Model):
User = models.ForeignKey(User, unique=True)
ProfImg = models.FileField(upload_to='images')
UserName = models.CharField(max_length=50)
def __unicode__(self):
return self.UserName
base.html
<html>
<head>
<title>
</title>
</head>
<body>
{% include '/login.html' %}
</body>
</html>
登录.html
{% if user.is_authenticated %}
<div class ="profile-info">
<img src="{{ MEDIA_URL }}{{ request.user.get_profile.ProfImg }}" width = "150" height = "150" />
Welcome {{ request.user.get_profile.UserName }}
<p>You last logged in on Tuesday the 19th of March, 2013 at 01:32pm.</p>
<p align="center"><a href="#">Profile</a> | <a href="#">Logout</a></p>
{% else %}
设置.py
AUTH_PROFILE_MODULE = "forums.UserProfile"
视图.py
def login_request(request):
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect("/")