Django视图:get_context_data()vs get()

时间:2022-11-13 19:17:40

We can do this in Django:

我们可以在Django中这样做:

class LoginView(generic.FormView):

    def get_context_data(self, **kwargs):
        common = CommonView(self, **kwargs)
        context = super(LoginView, self).get_context_data(**kwargs)
        context['common'] = common.infos

or:

class LoginView(generic.FormView):

    def get(self, request, *args, **kwargs):
        common = CommonView(self, **kwargs)
        return render(request, self.template_name, {'common': common.infos })

Which one is better and why?

哪一个更好,为什么?

2 个解决方案

#1


3  

Using get_context_data() is interesting since it has only one goal (do one thing and do it well): pass data to the template.

使用get_context_data()很有意思,因为它只有一个目标(做一件事并做得好):将数据传递给模板。

On the other hand, get() has many tasks to do in order to return an HttpResponse in the end. get() should rather be a skeleton of method calls, delegating tasks. Among them, you have get_context_data(), and others according to the class you are inheriting.

另一方面,get()有很多任务要做,以便最后返回一个HttpResponse。 get()应该是方法调用的骨架,委派任务。其中,你有get_context_data(),其他你根据你继承的类。

As long as it is possible, I think it is better to let the parent class(es) handle the get(), post(), ... methods and use the convenience methods they provide.

只要有可能,我认为最好让父类处理get(),post(),...方法并使用它们提供的方便方法。

As far as I'm concerned, the only case for which I had to handle the get() method myself is when writing generic views.

就我而言,我自己处理get()方法的唯一情况是编写通用视图。

#2


1  

None of them is "better". I'd say it depends on the scenario what you need to do. get_context_data() is called for all the request methods (post, get), so if you need to have there some data available every time, use get_context_data(). If you need the data only for a specific request method (eg. in get), then put it in get.

他们都不是“更好”。我会说这取决于您需要做的事情。为所有请求方法(post,get)调用get_context_data(),因此如果每次都需要一些数据,请使用get_context_data()。如果您只需要特定请求方法的数据(例如get),则将其放入get中。

#1


3  

Using get_context_data() is interesting since it has only one goal (do one thing and do it well): pass data to the template.

使用get_context_data()很有意思,因为它只有一个目标(做一件事并做得好):将数据传递给模板。

On the other hand, get() has many tasks to do in order to return an HttpResponse in the end. get() should rather be a skeleton of method calls, delegating tasks. Among them, you have get_context_data(), and others according to the class you are inheriting.

另一方面,get()有很多任务要做,以便最后返回一个HttpResponse。 get()应该是方法调用的骨架,委派任务。其中,你有get_context_data(),其他你根据你继承的类。

As long as it is possible, I think it is better to let the parent class(es) handle the get(), post(), ... methods and use the convenience methods they provide.

只要有可能,我认为最好让父类处理get(),post(),...方法并使用它们提供的方便方法。

As far as I'm concerned, the only case for which I had to handle the get() method myself is when writing generic views.

就我而言,我自己处理get()方法的唯一情况是编写通用视图。

#2


1  

None of them is "better". I'd say it depends on the scenario what you need to do. get_context_data() is called for all the request methods (post, get), so if you need to have there some data available every time, use get_context_data(). If you need the data only for a specific request method (eg. in get), then put it in get.

他们都不是“更好”。我会说这取决于您需要做的事情。为所有请求方法(post,get)调用get_context_data(),因此如果每次都需要一些数据,请使用get_context_data()。如果您只需要特定请求方法的数据(例如get),则将其放入get中。