html - How to do ModelName.objects.filter in html
问题描述
I'm trying to eliminate the amount of code I need to put in my view and want to do this in my html file:
{% for committee in c %}
{% for article in Article.objects.filter(committee=committee) %}
<a class="post-link" href="{% url 'update' id=article.id %}">{{article.title}}</a>
{% endfor %}
{% endfor %}
I am passing my article model and list of c as context in my view.
But it gives me this error:
TemplateSyntaxError at /admin-dash/
Could not parse the remainder: '(committee=committee)' from 'Article.objects.filter(committee=committee)'
Request Method: GET
Request URL: http://127.0.0.1:8000/admin-dash/
Django Version: 3.1.3
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '(committee=committee)' from 'Article.objects.filter(committee=committee)'
Exception Location: C:\Users\benja\anaconda3\lib\site-packages\django\template\base.py, line 662, in __init__
Python Executable: C:\Users\benja\anaconda3\python.exe
Python Version: 3.8.3
Python Path:
['C:\\Users\\benja\\Desktop\\mysite\\mysite',
'C:\\Users\\benja\\anaconda3\\python38.zip',
'C:\\Users\\benja\\anaconda3\\DLLs',
'C:\\Users\\benja\\anaconda3\\lib',
'C:\\Users\\benja\\anaconda3',
'C:\\Users\\benja\\anaconda3\\lib\\site-packages',
'C:\\Users\\benja\\anaconda3\\lib\\site-packages\\win32',
'C:\\Users\\benja\\anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Users\\benja\\anaconda3\\lib\\site-packages\\Pythonwin']
Server time: Tue, 12 Jan 2021 19:41:28 +0000
Error during template rendering
In template C:\Users\benja\Desktop\mysite\mysite\blog\templates\admin.html, error at line 18
Could not parse the remainder: '(committee=committee)' from 'Article.objects.filter(committee=committee)'
8 <link rel="stylesheet" href="{% static 'css/admin.css' %}">
9 <link rel="stylesheet" href="{% static 'css/posts.css' %}">
10 </head>
11 <main>
12
13
14 <div class="admin-panel">
15 <div class="posted">
16 <h1 style='font-size: 7rem; margin-bottom: 20px; margin-top: 0;'>Posts:</h1>
17 {% for committee in c %}
18 {% for article in Article.objects.filter(committee=committee) %}
19 <h1>committee</h1>
20 <a class="post-link" href="{% url 'update' id=article.id %}">{{article.title}}</a>
21 {% endfor %}
22 {% endfor %}
23 </div>
24 <div class="drafts">
25 <h1 style='font-size: 7rem; margin-bottom: 20px;'>Drafts:</h1>
26
27 <h1>Sustainability Committee:</h1>
28 {% for article in sc1 %}
Traceback Switch to copy-and-paste view
C:\Users\benja\anaconda3\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
response = get_response(request) …
▶ Local vars
C:\Users\benja\anaconda3\lib\site-packages\django\core\handlers\base.py, line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
C:\Users\benja\Desktop\mysite\mysite\blog\views.py, line 89, in admin_view
return render(request, 'admin.html', context)
Any idea how to do this? I think it's some sort of syntax. Thanks!
解决方案
推荐阅读
- azure - Docker 容器的 Web 应用程序已启动但也超时
- windows - Enable-PSRemoting 返回错误无法检查防火墙的状态
- ios - 使用可选值 Swift 4 解码嵌套 JSON
- javascript - 为什么 JavaScript 对象不会多次显示?
- python - tensorflow 导入错误:在 Python 2.7 中没有名为 tensorflowin Jupyter 的模块
- swift - 如何在悬停 NSTextView 上更改光标?斯威夫特 4,Xcode 9.4
- mysql - 无法打开引用的表
- xml - 使用 XSLT 1.0 更改 XML 输出结构
- javascript - 以英寸为单位的数量转换为英尺、厘米或码 Javascript
- php - 如何用js和每个项目的输出值制作一个可选择的项目菜单