object_detail() got multiple values for keyword argument 'queryset' while inputting only one

自闭症网瘾萝莉.ら 提交于 2019-12-12 01:27:57

问题


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})
    )

回答1:


The object_detail view has queryset as the first positional argument. So the value that matches (\S+) in your regex for that url is being interpreted as the queryset arg, which is conflicting with the kwarg you are passing in the POST dictionary.

If you're trying to send the object_id as the matching element in the URL, you'll need to use a named group:

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



回答2:


You need to add ?: to the groups (parentheses) that you don't want to be passed on to the view function. Like this:

url(r'^post/(?:\S+)/$', 'django.views.generic.list_detail.object_detail', post, name='post'),

See this article for more info: http://www.b-list.org/weblog/2007/oct/14/url-patterns/



来源:https://stackoverflow.com/questions/3387766/object-detail-got-multiple-values-for-keyword-argument-queryset-while-inputt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!