【问题标题】:How to do ModelName.objects.filter in html如何在 html 中执行 ModelName.objects.filter
【发布时间】:2021-01-13 03:50:08
【问题描述】:

我正在尝试消除需要放入视图中的代码量,并希望在我的 html 文件中执行此操作:

{% 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 %}

我正在传递我的文章模型和 c 列表作为我认为的上下文。

但它给了我这个错误:

    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) 

知道怎么做吗?我认为这是某种语法。谢谢!

【问题讨论】:

  • 分享您的Article 模型

标签: html django django-models


【解决方案1】:

您可以通过以下方式访问相关的Article 对象:

{% for committee in c %}
    {% for article in committee.article_set.all %}
        …
    {% endfor %}
{% endfor %}

如果你使用像ForeignKey 这样的关系,Django 会自动定义一个反向关系,对于ForeignKey,这将是<i>modelname</i>_set,所以在这种情况下article_set。您可以通过指定related_name=… parameter [Django-doc] 来反向更改关系的名称。

然而不是高效的,因为它会在这里进行N+1个查询:一个查询来获取Committees,并且对于每个Committee它会进行额外的查询以获取相关文章。

在视图中,您可以使用prefetch_related(…) [Django-doc] 通过一个额外的查询来获取相关对象,因此:

c = Committee.objects<b>.prefetch_related('article_set')</b>

我需要在我的视图中,并希望在我的 html 中执行此操作。

Django 模板被故意限制 允许使用参数进行函数调用。这是为了避免在模板中编写业务逻辑,这属于视图,而不是模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 2019-06-27
    • 2019-07-19
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多