pk通过但数据不显示
pk is passed but data not displayed
我有一个模型,其中包含一些判断及其标题。现在我想在点击相关标题时显示判断。我写了以下代码:
models.py
class Laws(models.Model):
date= models.DateField(default=timezone.now)
title= models.CharField(max_length=225, help_text="judgement title")
judgements= RichTextField(blank=True, null= True)
law_type= models.CharField(max_length=40, choices= type_of_law)
law_category= models.CharField(max_length=60, choices= category)
views.py
class DocView(ListView):
model= Laws
template_name = 'doc.html'
class Doc1DetailView(DetailView):
model= Laws
template_name = 'doc1.html'
urls.py
urlpatterns=[
path('doc', DocView.as_view(), name='doc' ),
path('doc1/<int:pk>', Doc1DetailView.as_view(), name='doc1' ),
]
并且 html 文件是
doc.html
{% for data in object_list %}
<div class="tab">
<a href="{% url 'doc1' data.pk %}">{{data.title}}</a><br/>
</div>
{% endfor %}
doc1.html
<p style="color: black;">{{data.judgements}}</p>
我参考了 youtube 上的一个视频并编写了这段代码,他的可以工作,而我的不行。我没有修改该代码中的任何内容,但数据仍未显示在 doc1.html 中。这里没有字段是空的。
请纠正我的代码并告诉我我做错了什么。
Laws
对象 不是 作为 data
传递的,而是作为 object
(以及 laws
)传递的,所以你渲染这个:
<p style="color: black;">{{ <strong>object.</strong>judgements }}</p>
Note: normally a Django model is given a singular name, so Law
instead of Laws
.
我有一个模型,其中包含一些判断及其标题。现在我想在点击相关标题时显示判断。我写了以下代码:
models.py
class Laws(models.Model):
date= models.DateField(default=timezone.now)
title= models.CharField(max_length=225, help_text="judgement title")
judgements= RichTextField(blank=True, null= True)
law_type= models.CharField(max_length=40, choices= type_of_law)
law_category= models.CharField(max_length=60, choices= category)
views.py
class DocView(ListView):
model= Laws
template_name = 'doc.html'
class Doc1DetailView(DetailView):
model= Laws
template_name = 'doc1.html'
urls.py
urlpatterns=[
path('doc', DocView.as_view(), name='doc' ),
path('doc1/<int:pk>', Doc1DetailView.as_view(), name='doc1' ),
]
并且 html 文件是 doc.html
{% for data in object_list %}
<div class="tab">
<a href="{% url 'doc1' data.pk %}">{{data.title}}</a><br/>
</div>
{% endfor %}
doc1.html
<p style="color: black;">{{data.judgements}}</p>
我参考了 youtube 上的一个视频并编写了这段代码,他的可以工作,而我的不行。我没有修改该代码中的任何内容,但数据仍未显示在 doc1.html 中。这里没有字段是空的。
请纠正我的代码并告诉我我做错了什么。
Laws
对象 不是 作为 data
传递的,而是作为 object
(以及 laws
)传递的,所以你渲染这个:
<p style="color: black;">{{ <strong>object.</strong>judgements }}</p>
Note: normally a Django model is given a singular name, so
Law
instead of.Laws