object_detail() 获得关键字参数“queryset”的多个值;当只输入一个时

发布于 2024-09-12 03:00:40 字数 1429 浏览 8 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-09-19 03:00:41

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

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

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

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+)/
 ...
戏蝶舞 2024-09-19 03:00:41

您需要将 ?: 添加到您不想传递给视图函数的组(括号)中。
像这样:

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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文