What is a context in Django?

后端 未结 3 1163
闹比i
闹比i 2020-11-28 03:37

I\'m a django beginner so I\'m trying to understand the concept of context and context processor.

  • What is a context and why do you use it?
  • Is a conte
3条回答
  •  醉话见心
    2020-11-28 04:30

    The Context is described quite well in the official documentation. In short:

    1. In day-to-day use, mostly indirectly, because helper functions construct the Context for you

    2. See 1.: you only need it if you use the low-level api

    3. No, a context processor is a function that takes a request and returns a dictionary of variables that are then available in all templates that are rendered with a RequestContext, for example:

      def get_stuff_from_session(request):
          return {'stuff': request.session['stuff']}
      

提交回复
热议问题