对于Django中的循环计数器,数字不显示

时间:2014-12-17 21:21:55

标签: python django

我希望在Django中使用for循环计数器来计算一到十的数字,但我没有看到任何数字。在查看文档时,我真的不明白我需要放入{{counter}}和{{value}}。

List.html

<p class="number">
    {% for key, value in data.items %}
        {{counter}}: {{value}}
    {% endfor %}
<p> 

models.py

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class FullArticleQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class FullArticle(models.Model):
    title = models.CharField(max_length=150)
    author = models.CharField(max_length=150)
    slug = models.SlugField(max_length=200, unique=True)
    pubDate = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    category = models.CharField(max_length=150)
    heroImage = models.CharField(max_length=250, blank=True)
    relatedImage =  models.CharField(max_length=250, blank=True)
    body =  models.TextField()
    publish = models.BooleanField(default=True)
    gameRank = models.CharField(max_length=150, blank=True, null=True)

    objects = FullArticleQuerySet.as_manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("FullArticle_detailed", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Blog entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-pubDate"]

views.py

from django.views import generic
from . import models 
from .models import FullArticle

# Create your views here.
class BlogIndex(generic.ListView):
    queryset = models.FullArticle.objects.published()
    template_name = "list.html" 
    randomArticle = FullArticle.objects.order_by('?').first()

class BlogDetail(generic.DetailView):
    model = models.FullArticle
    template_name = "detailed.html"

1 个答案:

答案 0 :(得分:2)

使用{{ forloop.counter }}而非{{ counter }}

此外,您永远不会在任何地方定义字典data。这应该是什么?你的查询集?