python - 试图在第一个 Django 项目中实现 HTML 模板。未放映的电影
问题描述
我正在关注一个在线 Python 教程,我必须创建一个 HTML 模板,在该模板中创建一个表格供最终用户查看库存中的电影。我一步一步地按照老师的指示,但是当我刷新浏览器页面时,它只显示了我在 HTML 中列出的类属性。我写的代码如下:
index.html 文件:
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Stock</th>
<th>Daily Rate</th>
</tr>
</thead>
<tbody>
{% for movie in movies %}
<tr>
<td>{{ movie.title }}</td>
<td>{{ movie.genre }}</td>
<td>{{ movie.number_in_stock }}</td>
<td>{{ movie.daily_rate }}</td>
</tr>
{% endfor %}
</tbody>
</table>
和 views.py 文件:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Movie
def index(request):
movies = Movie.objects.all()
return render(request, 'index.html', {' movies': movies})
这是我的网络浏览器上的结果:
如果有人知道为什么这不起作用,那么任何帮助都会很棒!
解决方案
您似乎有一个传递上下文的空间:
return render(request, 'index.html', {' movies': movies})
您需要替换' movies'
为'movies'
,否则在渲染模板时该变量将无法使用正确的名称。
推荐阅读
- kubernetes - 如何在 GKE Ingress 和它转发到的服务之间使用 HTTPS?
- python-3.x - 如何在使用 opencv 捕获时检测坏帧并忽略它们?
- angular - 即使我们可以用服务做所有事情,为什么我们还需要 NgRX?
- java - 在 Java 中读取 Docx/Doc 文件
- python-3.x - 如何使用 telnetlib 在 telnet 中按箭头键
- javascript - 如何在 Firebase 身份验证中发现错误
- debugging - 从 EXE 项目调试 DLL?
- android - React Native Expo android 文件夹在哪里?
- google-apps-script - 使用具有相同工作表名称但工作表顺序不同的应用脚本将多个 Google 工作表文件附加到一个 Google 工作表中
- google-sheets - 谷歌表格 - 选择多列进行数据验证