Django render() Function

Django render() Function

Django has a plethora of so-called shortcut functions that make developing with Django easier. These shortcuts are helper functions that span many different levels of the model, view, template paradigm. The render() function is one such function. The purpose of render() is to return an HttpResponse whose content is filled with the result of calling render_to_string() with the passed arguments. We saw how to use the render_to_string() function in the Register Django Templates tutorial. Now we’ll see how to make use of this easier-to-use render() function.

The render() Function

Before we make use of the render() function, let’s look at its source code.

def render(request, template_name, context=None, content_type=None, status=None, using=None):
    Return a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    content = loader.render_to_string(template_name, context, request, using=using)
    return HttpResponse(content, content_type, status)

The render() function has two mandatory arguments you must pass to it when calling the function. These are the request, and template_name parameters. Optional parameters include context, content_type, status, and using.

The Template To Render

Recall that we have this simple template shown below in the goals/templates/goals directory. It is considered a best practice to repeat the app name in the templates folder because if there are many apps in the project that have similar names, then Django would not know which templates below to which applications. This is because Django merges all template folders into one big templates folder during runtime.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>The Template Works!</h1>

Calling render() In A View Function

We already saw the long way of rendering a template in Django and that is to use the render_to_string() function to convert a template into a string that holds the HTML of the template. That string is then sent back to the user in an HttpResponse. The render() function lets us take that two-step process and complete the task in one step. The render() function is highlighted in the file below.


from django.shortcuts import redirect, render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

# dictionary data store
goals = {
    'daily': 'Learn Something New',
    'weekly': 'Take a day off',
    'monthly': 'Complete a creative course'

def homepage(request):
    goal_list = '<html><body><ul>'
    for goal in goals.keys():
        href = reverse('namedurl', args=[goal])
        goal_list += f'<li><a href="{href}">{goal}</a></li>'
    goal_list += '</ul></body></html>'
    return HttpResponse(goal_list)

def goals_by_int_timeframe(request, timeframe):
    timeframes = list(goals.keys())
    redirect_to = timeframes[timeframe - 1]
    named_redirect = reverse('namedurl', args=[redirect_to])
    return HttpResponseRedirect(named_redirect)

def goals_by_timeframe(request, timeframe):
    goal = goals[timeframe]
    return render(request, 'goals/goal.html')

Notice that we did pass the two required parameters of request and template_name. If we visit, it does work.

django render template