object_detail()获取关键字参数'queryset'的多个值,同时只输入一个

时间:2010-08-02 12:30:49

标签: python django django-queryset django-generic-views

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 :(得分:1)

object_detail视图将queryset作为第一个位置参数。因此,在您的正则表达式中为该URL匹配(\S+)的值将被解释为查询集arg,它与您在POST字典中传递的kwarg冲突。

如果您尝试将object_id作为URL中的匹配元素发送,则需要使用命名组:

url(r'^post/(?P<object_id>\S+)/$' ...

答案 1 :(得分: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/