Reputation: 116050
I've tried this several ways with no luck. If I try to render my view like this:
from django.shortcuts import render
from django.template import loader
def index(request):
render(request, loader.get_template('index.html'))
I get this error:
TemplateDoesNotExist at /
If I change the code to this:
from django.http import HttpResponse
from django.template.loader import render_to_string
def index(request):
content = render_to_string('index.html')
HttpResponse(content)
It actually finds the template and renders it (content
gets set to the rendered html) but I get this error now:
ValueError at /
The view home.controller.index didn't return an HttpResponse object.
Here is my folder structure and my settings:
myProject/
settings.py
home/
controller.py
urls.py
models.py
templates/
home/
index.html
Inside my setting.py file I have:
SITE_ROOT = os.path.dirname(os.path.realpath(__name__))
TEMPLATE_DIRS = ( os.path.join(SITE_ROOT, 'home/templates/home'), )
INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.staticfiles',
'gunicorn',
'home'
)
I've tried multiple variations for TEMPLATE_DIRS
but it's suppose to just pick it up correctly since I have home
added as an app I thought. Anyone know what's happening here?
UPDATE
A combination of things fixed this. First of all a return
statement is required (doh) and I think I was mixing examples of how to render a template. No need to import the loader or render it manually. This is what I wound up with that actually worked:
from django.shortcuts import render
def index(request):
return render(request, 'home/index.html')
Thanks to @lalo and @Rao for pointing me in the right direction.
Upvotes: 1
Views: 2018
Reputation: 2247
I would try that:
from django.shortcuts import render
def index(request):
render(request, 'home/index.html')
Upvotes: 2
Reputation: 892
You're not returning anything from your view.
return HttpResponse(blah)
Upvotes: 2