Как получить аннотацию для отображения данных в шаблоне django при загрузке страницы pastry_list.html?У меня есть переменная, установленная на строку аннотации в views.py.У меня есть цикл для запуска через переменную в шаблоне.Я что-то упускаю или делаю что-то не так?Почему-то кажется, что шаблон не распознает переменную views.py.Спасибо.
from django.db import models
from django.utils import timezone
from django.urls import reverse
from collections import OrderedDict
class IntegerRangeField(models.IntegerField):
def __init__(self, verbose_name=None, name=None, min_value=None,
max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
models.IntegerField.__init__(self, verbose_name, name, **kwargs)
def formfield(self, **kwargs):
defaults = {'min_value': self.min_value,
'max_value':self.max_value}
defaults.update(kwargs)
return super(IntegerRangeField, self).formfield(**defaults)
class Pastry(models.Model):
CHOICES_DICT = OrderedDict((
("Muffin", "Muffin"),
("Cupcake", "Cupcake"),
("Brownie", "Brownie"),
("Cake", "Cake"),
("Fruit Tart", "Fruit Tart")
))
pastry = models.CharField(max_length=20, default="muffin",
choices=CHOICES_DICT.items())
vote = IntegerRangeField(default=1, min_value=1, max_value=1)
def get_absolute_url(self):
return reverse("pastry_detail", kwargs={'pk': self.pk})
def __str__(self):
return self.pastry
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
ps = Pastry.objects.values('pastry').annotate(total=Count('pastry'))
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)
{% extends "base.html" %}
{% block content %}
<div class="jumbotron">
<a href="{% url 'pastry_new' %}">New Poll</a>
<h1>Voting for the favorite pastry</h1>
{% for p in ps %}
{% for k, v in p.items %}
{{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 %}
FORMS.PY
from django import forms
from myapp.models import Pastry
from django.forms import ModelForm
class PastryForm(ModelForm):
class Meta():
model = Pastry
fields = ['pastry', 'vote']
PASTRY_FORM.HTML
{% extends "base.html" %}
{% load widget_tweaks %}
{% block content %}
<div class="jumbotron">
<h1>New Poll</h1>
<form class="post-form" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
</div>
{% endblock %}
</body>
</html>