[英]how to display the nut,date fields in html template ? how to access foreign key fields in html tamplate?
I have three models in models.py which are Topic,Webpage and AccessRecord.我在models.py中有三个模型,它们是Topic、Webpage和AccessRecord。 class AccessRecord has foreign key relation with class Webpage i wanna use AccessRecord fields nut,date in html template which is used to display class Webpage content how we can access AccessRecord fields in which has a foreign relation with class Webpage ?类 AccessRecord 与类网页有外键关系我想在用于显示类网页内容的 html 模板中使用 AccessRecord 字段 nut,date 我们如何访问与类网页有外来关系的 AccessRecord 字段?
this is my model :这是我的模型:
class Topic(models.Model):
top_name = models.CharField(max_length=264,unique=True)
def __str__(self):
return self.top_name
class Webpage(models.Model):
topic = models.ForeignKey(Topic)
name = models.CharField(max_length=264,unique=True)
url = models.URLField(unique=True)
def __str__(self):
return self.name
class AccessRecord(models.Model):
name = models.ForeignKey(Webpage)
date = models.DateField()
nut = models.CharField(max_length=56)
def __str__(self):
return str(self.date)
this is my view :这是我的观点:
def index(request):
webpage_list = Webpage.objects.all()
web_dict = {"web_records":webpage_list}
return render(request,'first_app/index.html',web_dict)
this is my template :这是我的模板:
<body>
<h1>Hi, welcome to Django Level Two!</h1>
<h2>Here are your access records:</h2>
<div class="djangtwo">
{% if web_records %}
<table style="float: left">
<thead>
<th>Topic Name</th>
<th>Site name</th>
<th>Url Accessed</th>
<th>Date Accessed</th>
<th>Webpage nut</th>
</thead>
{% for web in web_records %}
<tr>
<td>{{ web.topic }}</td>
<td>{{ web.name }}</td>
<td>{{ web.url }}</td>
<td>{{ web.nut }}</td>
<td>{{ web.date }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</body>
You can also make views.py like你也可以让 views.py 像
#views.py
def index(request):
return render(request,'first_app/index.html')
AND modify template like:-并修改模板,如:-
<body>
<h1>Hi, welcome to Django Level Two!</h1>
<h2>Here are your access records:</h2>
<div class="djangtwo">
{% if web_records %}
<table style="float: left">
<thead>
<th>Topic Name</th>
<th>Site name</th>
<th>Url Accessed</th>
<th>Date Accessed</th>
<th>Webpage nut</th>
</thead>
<tr>
<td>{{ user.web.topic }}</td> #HERE
<td>{{ user.web.name }}</td> #HERE
<td>{{ user.web.url }}</td> #HERE
<td>{{ user.web.nut }}</td> #HERE
<td>{{ user.web.date }}</td> #HERE
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</body>
AND IF YOU WANT LOOP,SO YOU CAN EASILY USER LOOP.
You can also try this way:你也可以试试这种方式:
name
field to web
in AccessRecord
model.在AccessRecord
模型中将name
字段重命名为web
。models.py模型.py
class AccessRecord(models.Model):
web = models.ForeignKey(Webpage)
date = models.DateField()
nut = models.CharField(max_length=56)
def __str__(self):
return str(self.date)
AccessRecord
model instead of Webpage
model.获取AccessRecord
模型的所有对象,而不是Webpage
模型。views.py视图.py
from django.http import HttpResponse
from django.template import loader
def index(request):
access_records = AccessRecord.objects.all()
context = { "access_records": access_records }
t = loader.get_template('first_app/index.html')
return HttpResponse( t.render( context, request ) )
index.html索引.html
<body>
<h1>Hi, welcome to Django Level Two!</h1>
<h2>Here are your access records:</h2>
<div class="djangtwo">
{% if access_records %}
<table style="float: left">
<thead>
<th>Topic Name</th>
<th>Site name</th>
<th>Url Accessed</th>
<th>Date Accessed</th>
<th>Webpage nut</th>
</thead>
{% for access_record in access_records %}
<tr>
<td>{{ access_record.web.topic }}</td>
<td>{{ access_record.web.name }}</td>
<td>{{ access_record.web.url }}</td>
<td>{{ access_record.nut }}</td>
<td>{{ access_record.date }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.