在下面的代码中,而不是{{ i.user }}
我希望从{{ i.user }}
作为匹配值的另一个表中访问值。如何在HTML中完成它
{% for i in obj reversed %}
<div class="container">
<blockquote>
<header> {{ i.topic }} {{ i.user }} </header>
<p> {{ i.desc }} </p>
<footer> {{ i.time }} </footer>
</blockquote>
{% endfor %}
</div>
以下是我的模特
from django.db import models
class Accounts(models.Model):
name=models.CharField(max_length=30)
phone=models.CharField(max_length=20,unique=True)
mail=models.EmailField()
password=models.CharField(max_length=20)
def __str__(self):
return self.name
class BlogPost(models.Model):
user=models.CharField(max_length=30)
topic=models.CharField(max_length=100)
desc=models.CharField(max_length=1000)
likes=models.IntegerField(null=True,default=0)
time=models.DateTimeField(null=True)
def __str__(self):
return self.user
我希望Accounts
使用Blogspot.user
,即模板中的{{ i.user }}
获取价值
提前致谢...
答案 0 :(得分:1)
from django.contrib.auth.models import AbstractBaseUser
class Accounts(AbstractBaseUser):
name = models.CharField(max_length=30)
phone = models.CharField(max_length=20,unique=True)
mail = models.EmailField()
password = models.CharField(max_length=20)
def __str__(self):
return self.name
class BlogPost(models.Model):
user = models.ForeignKey(Accounts, related_name='accounts')
topic = models.CharField(max_length=100)
desc = models.CharField(max_length=1000)
likes = models.IntegerField(null=True,default=0)
time = models.DateTimeField(null=True)
def __str__(self):
return self.user
现在,使用外键,您可以访问模板中的帐户模型属性。