我可以使用用戶在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>
的login.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 %}
settings.py
AUTH_PROFILE_MODULE = "forums.UserProfile"
view.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("/")
我試過你的代碼但它沒有任何回報。我也試過'',但結果相同 – unice
您需要修復您的代碼 – catherine
感謝您的更正。我會嘗試你所說的。我稍後會回來看看結果。 – unice