将Extra_Context添加到App View(urls.py)

时间:2013-07-09 18:48:01

标签: django url view

我想添加一个额外的词典来使用therena的个人资料详细信息视图,但我无法弄清楚如何在我的urls.py中向视图添加extra_context。错误是VariableDoesNotExist

thing_list = {
    'queryset' : Thing.objects.all(),
    'template_object_name' : 'thing',
}

from userena import views as userena_views

url(r'^(?P<username>(?!signout|signup|signin)[\.\w-]+)/$',
   userena_views.profile_detail,
   {'extra_context':{'thing_list': thing_list}},
   name='userena_profile_detail'),

我的基础是这个问题的答案:

Adding extra_context in Django logout built-in view

感谢您的想法!

1 个答案:

答案 0 :(得分:2)

应该是

thing_list = {
    'queryset' : Thing.objects.all(),
    'template_object_name' : 'thing',
    'extra_context': {'swamp_things': Thing.objects.filter(type='swamp')},
}

url(r'^(?P<username>(?!signout|signup|signin)[\.\w-]+)/$',
   userena_views.profile_detail,
   thing_list,
   name='userena_profile_detail'),

至于您的最新评论(如何将请求纳入查询集过滤)。

<强> views.py

from django.views.generic import list_detail

def requestuserswampers(request):
    qs = Thing.objects.filter(user=request.user)
    return list_detail.object_list(
                request,
                queryset = Thing.objects.all(),
                template_object_name = 'thing',
                extra_context = {'swamp_things': qs},
    )

urls.py

from views import requestuserswampers

url(r'^(?P<username>(?!signout|signup|signin)[\.\w-]+)/$',
       requestuserswampers,
       name='userena_profile_detail'),

阅读Generic Views的文档非常好,它将教会你很多通用视图的实际工作方式以及你可以用它们做些什么!