简体   繁体   English

如何在Django模板中显示链接到带注释的查询集的views.py变量?

[英]How do you display a views.py's variable linked to annotated queryset in a django template?

How do you display a views.py's variable linked to annotated queryset in a django template? 如何在Django模板中显示链接到带注释的查询集的views.py变量? I know the annotated queryset returns correct data when I printed it out, but somehow the template for loop is not retrieving the data on the html page. 我知道带注释的查询集在打印出来时会返回正确的数据,但是以某种方式,循环模板无法在html页面上检索数据。 Can someone please advise me on how to fix this issue? 有人可以建议我如何解决此问题吗? Thanks. 谢谢。

VIEWS.PY 观点

from django.shortcuts import render
from django.views.generic import (TemplateView,ListView,
                              DetailView,CreateView,
                              UpdateView,DeleteView)
from django.urls import reverse_lazy
from myapp.models import Pastry
from myapp.forms import PastryForm
from django.db.models import F

This line ps = Pastry.objects.values('pastry').annotate(total=Count('pastry')) returns correct data: 这行ps = Pastry.objects.values('pastry').annotate(total=Count('pastry'))返回正确的数据:

{'pastry': 'Brownie', 'total': 1}
{'pastry': 'Cake', 'total': 1}
{'pastry': 'Cupcake', 'total': 1}
{'pastry': 'Fruit Tart', 'total': 1}
{'pastry': 'Muffin', 'total': 2}


class PollsListView(ListView):
    model = Pastry

    def get_queryset(self):
        return Pastry.objects.all()

class PollsDetailView(DetailView):
    model = Pastry

class PollsCreateView(CreateView):
    success_url = reverse_lazy('pastry_list')
    form_class = PastryForm
    model = Pastry

class PollsUpdateView(UpdateView):
    success_url = reverse_lazy('pastry_list')
    form_class = PastryForm
    model = Pastry

class PollsDeleteView(DeleteView):
    model = Pastry
    success_url = reverse_lazy('pastry_list')

pastry_list.html (template) pastry_list.html(模板)

{% extends "base.html" %}
{% block content %}
<div class="jumbotron">

<a href="{% url 'pastry_new' %}">New Poll</a>
<h1>Voting for the favorite pastry</h1>

Somehow this code here is not displaying any data.
{% for p in ps %}
 {% for k, v in p.items %}
   {{k}}{{v}}
 {% endfor %}
{% endfor %}

{% for pastry in pastry_list %}
    <div class="pastry">
        <h3><a href="{% url 'pastry_detail' pk=pastry.pk %}">
  {{ pastry.pastry }}</a></h3>
    </div>
  {% endfor %}

 </div>

 {% endblock %}

More info can be found in the Documentation 可以在文档中找到更多信息
Basically, you can send more variables to template via get_context_data() method 基本上,您可以通过get_context_data()方法将更多变量发送到模板

Sample: 样品:

class PollsListView(ListView):
    model = Pastry

    def get_queryset(self):
        return Pastry.objects.all()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['ps'] =  = Pastry.objects.values('pastry').annotate(total=Count('pastry'))
        return context

With get_context-data() , your variable ps is available in the template pastry_list.html 使用get_context-data() ,您的变量ps在模板pastry_list.html可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django模板中显示来自views.py的url变量 - Display url variable from views.py in Django template 如何从views.py中的Django模板获取变量的值? - How to get value in variable from Django template in views.py? 我如何在模板上显示我的 views.py 输出 - How do i display my views.py output on a template 在Django中,如何访问views.py中的模板值? - In Django, how do I access template values in the views.py? 如何将自定义类导入 Django views.py? - How do you import custom classes into Django views.py? 如何从 Django 中的 views.py 获取变量并将其显示在我的 HTML 页面上 - How do I take a variable from views.py in Django and display it on my HTML page django 如何将验证错误从 views.py 传递到 forms.py - django how do you pass validation errors from views.py to forms.py 如何使用Django queryset过滤器创建views.py以比较Django中两个不同表的特定值? - How do i create views.py with Django queryset filter to compare Specific value of two different tables in django? 如何使用 javascript 在 django 模板中设置变量并将其传递给 views.py? - How to set variable inside django template with javascript and pass it to views.py? 如何在views.py DJANGO中调用用户定义的函数 - How do you call a user defined function in views.py DJANGO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM