Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a context in Django?

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 context a value you need to have available in order to use in a template?
  • Is a context and a context processor the same?

I'll apreciate a lot your response. Thanks in advance!

like image 678
Camilo Sanchez Avatar asked Jan 06 '14 19:01

Camilo Sanchez


People also ask

What is context in Render?

What is the render context? The class RenderContext is automatically created by the graphics service. It is a collection of data that needs to be passed to every Render method. The render context should contain all information required to render an object or to perform a rendering step.

What is a template context?

The context template data file provides the content for establishing the context. It is usually a JAR or ZIP file that contains a specification XML file, the template DTD, business XML files for defining rules, and content files for any objects that are identified in the specification.

What is render in Django?

Rendering means interpolating the template with context data and returning the resulting string. The Django template language is Django's own template system. Until Django 1.8 it was the only built-in option available. It's a good template library even though it's fairly opinionated and sports a few idiosyncrasies.

What is Django template language?

What is Django Template Language? Django Template Language or DTL is a text-based Template language that provides a bridge between scripts like HTML, CSS, JS, etc. and programming languages like python. DTL is specifically built for developers to embed Django logic codes into HTML template files.


2 Answers

When you use a Django Template, it is compiled once (and only once) and stored for future use, as an optimization. A template can have variable names in double curly braces, such as {{ myvar1 }} and {{ myvar2 }}.

A Context is a dictionary with variable names as the key and their values as the value. Hence, if your context for the above template looks like: {myvar1: 101, myvar2: 102}, when you pass this context to the template render method, {{ myvar1 }} would be replaced with 101 and {{ myvar2 }} with 102 in your template. This is a simplistic example, but really a Context object is the context in which the template is being rendered.

As for a ContextProcessor, that is a slightly advanced concept. You can have in your settings.py file listed a few Context Processors which take in an HttpRequest object and return a dictionary (similar to the Context object above). The dictionary (context) returned by the Context Processor is merged into the context passed in by you (the user) by Django.

A use case for a Context Processor is when you always want to insert certain variables inside your template (for example the location of the user could be a candidate). Instead of writing code to insert it in each view, you could simply write a context processor for it and add it to the TEMPLATE_CONTEXT_PROCESSORS settings in settings.py.

Hope this makes sense. Thanks for taking the class!

like image 154
Sid Avatar answered Oct 06 '22 13:10

Sid


A context is a variable name -> variable value mapping that is passed to a template.

Context processors let you specify a number of variables that get set in each context automatically – without you having to specify the variables in each render() call.

like image 33
Victor Castillo Torres Avatar answered Oct 06 '22 11:10

Victor Castillo Torres