object_detail() 的 'queryset' 关键字参数传入了多个值
from django.conf.urls.defaults import *
from django.conf import settings
from Website.Blog.models import Post
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
index = {
'queryset': Post.objects.all(),
'date_field': 'created_on',
'template_name': 'index.html',
'num_latest': 5
}
post = {
'template_name': 'index.html',
'queryset': Post.objects.all(), # only here, what could be wrong?
'slug': 'slug',
}
urlpatterns = patterns('',
# Example:
url(r'^$', 'django.views.generic.date_based.archive_index', index, name='index'),
url(r'^post/(\S+)/$', 'django.views.generic.list_detail.object_detail', post, name='post'),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls))
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^css/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.IMAGES_ROOT, 'show_indexes': True})
)
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
2 个回答
0
你需要在不想传递给视图函数的分组(括号)前面加上 ?:
。这样做:
url(r'^post/(?:\S+)/$', 'django.views.generic.list_detail.object_detail', post, name='post'),
想了解更多信息,可以查看这篇文章: http://www.b-list.org/weblog/2007/oct/14/url-patterns/
1
object_detail
这个视图的第一个参数是 queryset
。所以在你的网址中,正则表达式 (\S+)
匹配到的值被当作 queryset
的参数来处理,这就和你在 POST 字典中传递的关键字参数发生了冲突。
如果你想在网址中发送 object_id
作为匹配的元素,你需要使用命名组:
url(r'^post/(?P<object_id>\S+)/$' ...