Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django基于泛型类的视图示例:**kwargs来自哪里?_Django - Fatal编程技术网

Django基于泛型类的视图示例:**kwargs来自哪里?

Django基于泛型类的视图示例:**kwargs来自哪里?,django,Django,在这些例子中,我经常看到**kwargs被传来传去,没有提到它来自哪里: from django.views.generic import DetailView from books.models import Publisher, Book class PublisherDetailView(DetailView): context_object_name = "publisher" model = Publisher def get_context_data(s

在这些例子中,我经常看到**kwargs被传来传去,没有提到它来自哪里:

from django.views.generic import DetailView
from books.models import Publisher, Book

class PublisherDetailView(DetailView):

    context_object_name = "publisher"
    model = Publisher

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PublisherDetailView, self).get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['book_list'] = Book.objects.all()
        return context
魔术师从哪里拔出**克瓦格


另外,仅仅添加一个dictionary对象似乎不需要很多额外的工作吗?

Kwarg是在中生成的。例如,这将填充
pk
项:

urlpatterns = patterns('',
    (r'^authors/(?P<pk>\d+)/$', AuthorDetailView.as_view()),
)
urlpatterns=patterns(“”,
(r“^authors/(?P\d+/$”,AuthorDetailView.as_view()),
)

调用是通过
视图中的函数进行的。as\u View
然后通过
视图进行调用。dispatch
哪个调用。

查看
SingleObjectMixin的基本实现(“原始”
获取上下文数据

它只返回
**kwargs
作为上下文(字典),同时添加使用指定键编辑的对象

 def get_context_data(self, **kwargs):
            context = kwargs
            context_object_name = self.get_context_object_name(self.object)
            if context_object_name:
                context[context_object_name] = self.object
            return context
DetailView
中,KWARG被“神奇地从”任何调用它/传递这些KWARG的东西中提取出来。在您的情况下,这将是
BaseDetailView.get()

它后来被许多视图类使用(如
render\u to\u response(self.get\u context\u data)
),它将原始
context
字典传递给
self.response\u类
,默认情况下
django.template.templaterresponse

TemplateResponse
知道如何呈现自己,并在其
resolve_context
函数中,最终将字典转换为
django.template.context


您真的可以从原始方法一直跟踪源代码。

可能的重复:我说的是这个特定实例。我知道**kwargs是什么意思。
class BaseDetailView(SingleObjectMixin, View):
        def get(self, request, **kwargs):
            self.object = self.get_object()
            context = self.get_context_data(object=self.object)
            return self.render_to_response(context)