Django Python: global name 'render' is not defined

Question:

I am getting an error in my Django project, and it looks like it’s coming from my views.py file:

from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import datetime

def get_date_time(request):
    now = datetime.datetime.now()
    return render(request, 'date_time.html', {'current_date': now})

Error: global name 'render' is not defined

What can I do to solve this?

Asked By: TheProofIsTrivium

||

Answers:

You need to import render from django.shortcuts as it is not a built-in function.:

from django.shortcuts import render
Answered By: Ashwini Chaudhary

If you are following the Django tutorial and have this error but already have the import, it could be because the web server needs to be reloaded. The changes in code won’t be reflected until runserver is ran again.

Answered By: Wes

Solution:

t = get_template('document.html')
html = t.render(Context({'variable': value}))
return HttpResponse(html)

This answer was posted as an edit to the question Django Python: global name 'render' is not defined by the OP TheProofIsTrivium under CC BY-SA 3.0.

Answered By: vvvvv
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.