仅输入一个时,object_detail()的关键字参数“ queryset”有多个值 [英] object_detail() got multiple values for keyword argument 'queryset' while inputting only one

查看:116
本文介绍了仅输入一个时,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})
    )


推荐答案

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

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.

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

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+)/$' ...

这篇关于仅输入一个时,object_detail()的关键字参数“ queryset”有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆