I have a variable in the file views.py
of my app directory.
I need to print that variable inside my template.
How can I do That?

- 159
- 4
- 12
-
Why Down votes? – kashish Jun 14 '17 at 10:01
-
Because this is an absolutely basic question which is answered with a simple read of the tutorial. – Daniel Roseman Jun 14 '17 at 10:09
-
@DanielRoseman Isn't [this](https://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty?rq=1) a basic question too. Why then so many upvotes? – kashish Jun 15 '17 at 11:13
1 Answers
You can pass the variable into context dictionary and render it in the template,
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 }}, {{ 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.
An example,
In your views.py
foo = 'bar'
context = {'foo': foo }
return render(request, 'template_name', context)
And in your template, you can access the variable,
{{ foo }}

- 9,017
- 2
- 24
- 47
-
-
`foo = 'bar'` means foo is the variable name, 'bar' is just a string – zaidfazil Jun 14 '17 at 10:02
-
-
`'bar'` is a string. `foo` is a variable which holds the string value. Then, if you want to print the variable in the template, look at my answer. do what it says. – zaidfazil Jun 14 '17 at 10:04
-
@kashish , context = {'foo': foo } you can say this, context = {'foo': 'bar' } he divide it for your understand. because you said its variable thats why – Mohideen bin Mohammed Jun 14 '17 at 10:07
-
@MohideenibnMohammed Thanks for comment. Why string `bar`? Why not something else. – kashish Jun 14 '17 at 10:10
-
It could be anything, I said its just an example.... You can change it whatever you please..... – zaidfazil Jun 14 '17 at 10:11
-
-
-
you could do the same, pass a context with a dictionary like above.... and loop inside it in the template.. – zaidfazil Jun 16 '17 at 06:02