Reputation: 808
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?
Upvotes: 8
Views: 21273
Reputation: 31619
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.
Upvotes: 0
Reputation: 866
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.
Upvotes: 1
Reputation: 250951
You need to import render
from django.shortcuts
as it is not a built-in function.:
from django.shortcuts import render
Upvotes: 21