Search code examples
djangorender

What does the "context" argument to the render() function in Django do?


I am working through the basic Blog tutorial on Django and After creating an app I have to add it to the veiw, What I don't understand is what the render functions' argument CONTEXT is for, and why are we using a dictionary.

I have already been through the official documentation but I couldn't understand it.

Here's what I am doing.

    render(request,'users/register.html', { 'form': form})

Solution

  • It supplies the variables to display in the template.

    For example, if your template had this bit of html:

    <p>Hello {{ first_name }}.</p>
    

    And if you pass the first_name variable in the context:

    render(request,'users/register.html', {'form': form, 'first_name': 'John'})
    

    The template would display Hello John.