我知道這是在Django一個很基本的概念,我已經嘗試了教程,但它不工作。我的工作與這樣設置的模型一本漫畫書數據庫(至少兩個型號的樣品):在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(
)),
當然,去/不可思議-X戰警-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的新手,所以簡單地告訴我再次查看教程是不會有幫助的。所以,也許寫出代碼會有所幫助!謝謝!
謝謝你,@piquadrat。儘管出於某種原因,這種方法很有效,但該模板不是指向issues_by_title.html,而是指向issue_list.html。我沒有在我的視圖或URL中的任何地方使用issue_list,並且我已經將所有實例註釋掉了! – AAA 2011-04-17 18:55:30