【问题标题】:Set Custom Header Class Based View Django设置基于自定义标题类的视图 Django
【发布时间】:2016-10-16 19:19:17
【问题描述】:

我正在学习 Django 中的基于类的视图,并且已经学会使用基本的通用视图,如 View 和 TemplateView。

我正在使用像 ListView 和 DetailView 这样的通用视图,然后我偶然发现了一个问题。 我们如何在继承自 ListView 和 DetailView 等视图类之一的基于类的视图中添加自定义标题。

我搜索了基于函数的视图的所有答案。

我已经能够在继承自 View 类的 Class Base Views 中设置标题。

class MyView(View):
    http_method_names=['post','get']
    message='<div id="myview">This is a class based view response.</div>'
    content_type='text/html'
    charset='utf-8'
    template_name='base.html'

    @method_decorator(gzip_page)
    @method_decorator(condition(etag_func=None,last_modified_func=None))
    def get(self,request,*args,**kwargs):
        response=TemplateResponse(request,self.template_name,{'number':1,'number_2':2})
        response.__setitem__('x-uuid',uuid.uuid4().hex)     #set header explicitly
        response.__setitem__('status',200)
        response.__setitem__('page_id',str(uuid.uuid4().hex))
        patch_vary_headers(response,['Etag','Cookie','User-Agent'])
        patch_cache_control(response)
        return response

    #Override this method to tell what to do when one of the methods in http_method_names is called
    def http_method_not_allowed(request, *args, **kwargs):
        response=HttpResponse("Method not allowed",status=405)
        response.__setitem__('x-uid',uuid.uuid4().hex)      #set header explicitly
        response.__setitem__('status',405)
        response.__setitem__({'hello':'word'})
        return response
        #return any type of response here
    # return JsonResponse(status=405,data={'not_allowed':True})

谁能告诉我如何在继承自 ListView 或任何其他视图(如 DetailView)的基于类的视图中添加自定义标题。

 class GetParticularUserView(ListView):
     http_method_names=['get']
    template_name='one_user.html'
    # context_object_name='user'        # set context either by this or by get_context_data
    '''queryset=User.objects.all()'''   # one way to set the data in the context

    def get_queryset(self):
        # define the query set to be used here.
        self.user=get_object_or_404(User,id=self.kwargs['id']) 
        return self.user

    def get_context_data(self,**kwargs):
         context=super(GetParticularUserView,self).get_context_data(**kwargs)
        context['user']=self.user
        return context

【问题讨论】:

    标签: django http django-views django-class-based-views


    【解决方案1】:

    我会覆盖dispatch 方法。调用super() 获取响应,设置标题,然后返回响应。请注意,您不需要调用 __setitem__ - 只需将响应视为字典即可。

    class GetParticularUserView(ListView):
    
        @method_decorator(gzip_page)
        @method_decorator(condition(etag_func=None,last_modified_func=None))
        def dispatch(self, *args, **kwargs):
            response = super(GetParticularUserView, self).dispatch(*args, **kwargs)
            response['x-uid'] = uuid.uuid4().hex  # set header
            return response
    

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 2014-07-14
      相关资源
      最近更新 更多