【发布时间】:2011-04-17 16:03:55
【问题描述】:
我知道这是 Django 中的一个非常基本的概念,我已经尝试过该教程,但它不起作用。我正在使用这样的模型设置漫画书数据库(至少是其中两个模型的样本):
Class Title(models.Model):
title = models.CharField(max_length=256)
vol = models.IntegerField("Vol.")
slug = models.SlugField(blank=True, null=True)
#desc = models.CharField(max_length=256)
class Meta:
ordering = ['title']
def get_absolute_url(self):
return "/comics2/title/%s" % self.slug
def __unicode__(self):
return self.title
class Issue(models.Model):
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Enter the number only. Do not include the hashtag.")
writer = models.ManyToManyField(Creator)
我要做的是创建一个页面,显示该标题中所有问题的列表。
但是,我在这样的视图中设置了它:
class AstonishingXMenIssueListView(ListView):
context_object_name = "astonishing_list"
queryset = Issue.objects.filter(title__title="Astonishing X-Men").order_by("number")
template_name = "comics2/astonishing_list.html"
我的 urls.py 看起来像这样:
(r'^comics2/title/(?P<title_slug>[-\w]+)/$', AstonishingXMenIssueListView.as_view(
)),
当然,转到 /uncanny-xmen-v1/ 显示的内容与上面的惊人链接相同。
显然,这不是按标题列出未来问题和标题的问题的实用方法,因此我需要设置它,这样我就不必单独执行此操作。现在,我尝试按照 Django 通用视图教程进行操作,但出现索引元组错误。
我试过这个,但它不起作用。这就是让我得到索引元组错误的原因。
class IssuesByTitleView(ListView):
context_object_name = "issues_by_title_list"
template_name = "comics2/issues_by_title.html",
def get_queryset(self):
title = get_object_or_404(Title, title__iexact=self.args[0])
return Issue.objects.filter(title=title)
有什么想法吗?有人可以用婴儿语言回复吗,因为我是 Django 和 Python 的新手,所以简单地告诉我再看一下教程是没有帮助的。所以,也许写出代码会有所帮助!谢谢!
【问题讨论】:
标签: django