How to pass variables from the Django view to a template

Django Bindings has been deprecated. For more, see the Deprecation Notice.

To pass variables from a Django view function to a template, use the return{variable} method in the view. On the template, use the variable name in double curly brackets: {{ variable }}.

For example, to define a message variable that contains some text that you want to return to a page template, use the following code in your app's views.py file to pass the variable:

@render_to('your_app_name:template_name.html')
@login_required
def view_name(request):
    return {
        "message": "Here is some text"
    }

Then, use this code in the app's template to display the message variable:

Here is the message variable from my view definition: {{ message }}

The views.py is a Python module, so you can include code in the view. Here's an example of a view that sends a today variable with the current date as a string to the template:

import datetime
...
@render_to('your_app_name:template_name.html')
@login_required
def view_name(request):
    now = datetime.datetime.now()
    today = now.strftime("%A, %b %d, %Y")
    return {
        "today": today
    }