0
我正在爲我的django應用程序創建一個自定義用戶和配置文件模型。我創建了一個用戶模型和另一個用戶模型OneToOneField
的模型。 我想要在模板中獲取配置文件模型字段值,但只有我可以從當前登錄的用戶獲取視圖中的用戶模型對象。我如何才能在模板中獲取配置文件模型字段值?獲取繼承類字段django
models.py
class UserSignup(models.Model):
email = models.CharField(unique=True, max_length=254)
name = models.CharField(max_length=100)
password = models.CharField(max_length=1000)
created_date = datetime.now()
def __str__(self):
return self.email
class UserProfile(models.Model):
user = models.OneToOneField(UserSignup, on_delete=models.CASCADE)
full_name = models.CharField(max_length=200, null=True)
#photo = models.ImageField(upload_to="/user/image/", blank=True)
birth_date = models.DateField(null=True)
books_wrote = models.TextField(null=True)
journal_wrote = models.TextField(null=True)
address = models.TextField(max_length=2000, null=True)
@receiver(post_save, sender=UserSignup)
def update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
views.py
def user_profile(request):
op = UserSignup.objects.get(id=request.session['uid'])
#here i want object of profile so i can access fields of profile in template
return render(request, 'editor_profile.html', {'op': op})
模板
<div class="panel-heading">
<h3 class="panel-title">{{ op.name }}</h3>
</div>
<div class=" col-md-9 col-lg-9 ">
<table class="table table-user-information">
<tbody>
<tr>
<td>Full Name:</td>
<td>{{ op.full_name }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
你試過'op.userprofile.full_name'嗎? –
您在這裏沒有繼承關係。 –
我提供了錯誤的模板,@SardorbekImomaliev你的方法工作表示感謝。 – rohillasarthak