Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django过滤器查询集模型_Django_Django Models - Fatal编程技术网

Django过滤器查询集模型

Django过滤器查询集模型,django,django-models,Django,Django Models,如何筛选表以仅显示项目中的引号 为了显示所有引号,我使用{%作为引号中的引号。all%} 现在,我想显示相对于站点的报价。也就是说,在选择我的站点id时,我只能看到与此站点id相关的报价。我可以编写如下内容 {% for quote in quotes.all %} {% if quote chantier.id %} {% endif %} {% endfor %} 但这是错误的 以下是我的报价模型: 型号.py class Quote(models.Model):

如何筛选表以仅显示项目中的引号

为了显示所有引号,我使用
{%作为引号中的引号。all%}

现在,我想显示相对于站点的报价。也就是说,在选择我的站点id时,我只能看到与此站点id相关的报价。我可以编写如下内容

{% for quote in quotes.all %}
    {% if quote chantier.id %}
    {% endif %}
{% endfor %}
但这是错误的

以下是我的报价模型:

型号.py

class Quote(models.Model):
    name = models.CharField(max_length=30)    
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
如何显示此网站的所有报价


非常感谢,

您可以创建包含
站点id的url路径

# app_name/urls.py

from django.urls import path
from app_name import views

urlpatterns = [
    path('site/<int:site_id>/', views.quotes_of_site, name='quotes_of_site'),
    # …
]
# app_name/views.py

from django.shortcuts import render
from app_name.models import Quote

def quotes_of_site(request, site_id):
    quotes = Quote.objects.filter(site_id=site_id)
    return render(request, 'name_of_template.html', {'quotes': quotes})
然后迭代此
查询集

{% for quote in quotes %}
    …
{% endfor %}

这个
chantier
来自哪里?