python - 无法在模板中显示模型中的数据
问题描述
我正在尝试显示我内部模型中的数据,该模型post.html
以前有效,但现在由于某种原因我无法弄清楚。
网址.py
urlpatterns = [
path('', ListView.as_view(
queryset=Tutorials.objects.all().order_by("-date")[:25],
template_name="tutorials/blog.html"
)),
path('<int:pk>', DetailView.as_view(
model=Tutorials,
template_name="tutorials/post.html")),
]
博客.html
{% block python %}
{% for Tutorials in object_list %}
{% if Tutorials.categories == "pythonbasics" %}
<a href="/tutorials/{{ Tutorials.id }}"><h1>{{ Tutorials.title }}</h1></a>
<br>
Created On : {{ Tutorials.date|date:"Y-m-d" }}
{% endif %}
{% endfor %}
{% endblock %}
post.html
{% block content %}
<h3>{{ Tutorials.title }}</h3>
<h6> on {{ Tutorials.datetime }}</h6>
<div class = "code">
{{ Tutorials.content|linebreaks }}
</div>
{% endblock %}
解决方案
您应该object
在模板中使用名称:
{% block content %}
<h3>{{ object.title }}</h3>
<h6> on {{ object.datetime }}</h6>
<div class = "code">
{{ object.content|linebreaks }}
</div>
{% endblock %}
或者如果你想使用Tutorial
变量,你需要传递context_object_name=Tutorials
给视图:
path('<int:pk>', DetailView.as_view(
model=Tutorials,
template_name="tutorials/post.html",
context_object_name='Tutorials')),
推荐阅读
- android - Android Studio 3.1.3 错误:无法解决依赖关系
- python-2.7 - python,在for循环中我只能用我的最后一个元素进行迭代
- java - 如何在 Jtable 中动态添加列名称?
- javascript - java - 如何在java脚本中仅使用am和pm获取小时数
- php - 使用 xPath 选择特定标签的所有子代
- c# - .net 核心中hangfire RecurringJob 的入口点
- javascript - 如何通过 JavaScript 动态隐藏第一个“N”个 DT 和 DD 标签?
- ios - 如何在 iOS swift 3 中创建单独的文件夹
- protractor - 如何在 Firefox 和 IE 浏览器的量角器脚本中处理文件下载对话框
- node.js - 未找到模块:无法从 Material-Ui 解析“@babel/runtime/helpers/builtin/classCallCheck”