我的models.py
文件看起来像这样
from django.db import models
from django.template.defaultfilters import slugify
class Entertainmentblog(models.Model):
slug = models.SlugField(max_length=100)
body = models.TextField()
posted = models.DateTimeField('date published')
img_url0 = models.CharField(max_length=100)
img_alt0 = models.CharField(max_length=100)
title1 = models.CharField(max_length=100)
title2 = models.CharField(max_length=100)
def save(self):
super(Entertainmentblog, self).save()
self.slug = '%i-%s' % ( self.id, slugify(self.slug) )
super(Entertainmentblog, self).save()
我的应用urls.py
文件看起来像这样
from django.conf.urls import patterns, url
from entertainment import views
urlpatterns = patterns('',
url(r'^$', views.ListView.as_view(), name='index'),
url(r'^(?P<slug>[^\.]+),(?P<id>\d+)/$', views.DetailView.as_view(), name='article'),
)
但这会产生错误。
异常值:反向文章&#39;有争议的&#39;(你正在发生的事情&#39;,&#39;和关键字参数&#39; {}&#39;未找到。尝试了1种模式:[u&#39;娱乐/(?P [^ \。] +),(?P \ d +)/ $&#39;]
我的view.py
文件
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from entertainment.models import Entertainmentblog
class ListView(generic.ListView, slug, id):
template_name = 'entertainment/index.html'
context_object_name = 'latest_article_list'
def get_queryset(self):
return Entertainmentblog.objects.order_by('-posted')[:25]
class DetailView(generic.DetailView):
model = Entertainmentblog
template_name = 'entertainment/article.html'
我该如何纠正?
答案 0 :(得分:2)
哦,你的观点存在严重问题:
class ListView(generic.ListView, slug, id)
应该是
class ListView(generic.ListView)
slug
和id
必须是您视图的类成员才能重新定义您的视图:
class ListView(generic.ListView):
template_name = 'entertainment/index.html'
context_object_name = 'latest_article_list'
slug = None
id = None
def get_queryset(self):
return Entertainmentblog.objects.order_by('-posted')[:25]
您将派生类命名为其父级。我不知道这样做的含义,但肯定不是一个好的做法。
您获得的错误是因为views.DetailView.as_view()
返回的视图(记住DetailView
是您的派生类)不接收您通过url传递的参数。检查你的网址,我可以看到抱怨的错误和参数(u'what-is-happening',)
,但没有id
。它应该类似于(u'what-is-happening', '4')