【问题标题】:django : get base variables before view starts workdjango:在视图开始工作之前获取基本变量
【发布时间】:2012-10-28 16:16:00
【问题描述】:

我有一个这样的问题,我搜索了很多,阅读了decoratorsmiddleware,但我没有明白如何更好地解决我的问题。

我有基本模板base.html 和模板template1.htmltemplate2.html,它们是base.html 的扩展。

base.html 有一些通用块,在template1.htmltemplate2.html 中需要。 这个块是动态数据,所以我必须在每个视图中获取这个块的数据,然后渲染模板。

例如我有 2 个views

@render_to("template1.html")
def fun_1(request):
data = getGeneralData()
#...getting other data
return {
        "data" : data,
        "other" : other_data,
}

@render_to("template2.html")
def fun_2(request):
data = getGeneralData()
#...getting other data
return {
        "data" : data,
        "other2" : other_data2,
}

所以一般来说,我在我的所有views 中都需要这个getGeneralData,让我在我的每个视图中调用getGeneralData() 函数,或者我可以制作任何将获得general_data 的函数并将其渲染到模板之前视图获取自己的数据?

您能否给我一个代码示例或给我一个很好的链接如何做得更好?

【问题讨论】:

    标签: django view decorator


    【解决方案1】:

    建议您编写自己的 context processor 并从那里返回所需的上下文数据。

    例子:

    custom_context_processor.py

    def ctx_getGeneralData(request):
        ctx = {}
        ctx['data'] = getGeneralData()
        return ctx
    

    在设置文件中,将TEMPLATE_CONTEXT_PROCESSORS 更新为'custom_context_processor.ctx_getGeneralData'

    【讨论】:

      【解决方案2】:

      您是否考虑过使用 Django 的基于类的视图?它们最初有点难以使用,但它们使这样的事情变得非常简单。以下是我如何使用基于类的视图重写基于函数的视图:

      # TemplateView is a generic class based view that simply
      # renders a template.
      from django.views.generic import TemplateView
      
      
      # Let's start by defining a mixin that we can mix into any view that
      # needs the result of getGeneralData() in its template context.
      
      
      class GeneralContextDataMixin(object):
          """
          Adds the result of calling getGeneralData() to the context of whichever
          view it is mixed into.
          """
      
          get_context_data(self, *args, **kwargs):
              """
              Django calls get_context_data() on all CBVs that render templates.
              It returns a dictionary containing the data you want to add to
              your template context.
              """
              context = super(GeneralContextDataMixin, self).get_context_data(*args, **kwargs)
              context['data'] = getGeneralData()
              return context
      
      
      # Now we can inherit from this mixin wherever we need the results of
      # getGeneralData(). Note the order of inheritance; it's important.
      
      
      class View1(GeneralContextDataMixin, TemplateView):
          template_name = 'template1.html'
      
      
      class View2(GeneralContextDataMixin, TemplateView):
          template_name = 'template2.html'
      

      当然,您也可以像 Rohan 所说的那样编写自己的上下文处理器。事实上,如果您想将此数据添加到您的所有视图中,我建议您这样做。

      无论你最终做了什么,我都会敦促你研究基于类的视图。它们使许多重复性任务变得非常容易。

      进一步阅读:

      【讨论】:

        猜你喜欢
        • 2019-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        相关资源
        最近更新 更多