我有CustomUser
类用于日志记录,还有另一个coach
类用于记录用户的信息。每个用户都链接到coach类。
我想创建模板,我的用户,如果他们完成了他们的个人资料将看到他们的信息,如果他们没有完成他们的个人资料,他们将看到一条消息。我正在学习pythondjango,例如,我不知道如何从类coach
中获取我登录的用户的Adresse
,并检查它是否为空。
你知道怎么解决吗?你知道吗
我的视图.py你知道吗
d级
def Profile(request):
u = request.user.username
x = u.coach.Adresse
if len(x)!= 0:
completed = "profile completed"
return render(request, 'Profile.html', {'completed': completed})
else:
notcompleted = "please complete your profile"
return render(request, 'Profile.html', {'notcompleted': notcompleted})
我的型号.py你知道吗
class coach(models.Model):
user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
Adresse = models.TextField(max_length=140, default='DEFAULT VALUE')
Telephone = models.IntegerField(null=True, max_length=140, default='111')
Django推荐的最不痛苦的方法是通过
OneToOneField(User)
属性。你知道吗也就是说,扩展
django.contrib.auth.models.User
并替换它也能起作用。。。你知道吗不是编写自己的用户类,而是扩展现有的用户模型。。你知道吗
您的请求将自动添加到模板中,这意味着您可以在模板中访问它,而无需在上下文中传递它。它由
render
函数传递给模板。你知道吗所以在
Profile.html
模板中写下这个:并将
Profile
方法更改为但是我认为你应该改变adrese字段,因为我看不出你为什么要使用默认值。只需删除默认值并允许其为空,如下所示:
在模板中而不是
{% if request.user.coach.Adresse != 'DEFAULT VALUE' %}
写{% if request.user.coach.Adresse %}
相关问题 更多 >
编程相关推荐