How to return a static HTML file as a response in Django?
You are not calling the render
method there, are you?
Compare:
template.render
template.render()
By using HttpResponse
you can send data only, if you want to html file then you have to use render
or render_to_response
by following ways...
from django.shortcuts import render
def index(request):
return render(request, 'app/index.html')
or
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
If your file isn't a django template but a plain html file, this is the easiest way:
from django.shortcuts import render_to_response
def index (request):
return render_to_response('app/index.html')
UPDATE 10/13/2020:
render_to_response
was deprecated in Django 2.0 and removed in 3.0, so the current way of doing this is:
from django.shortcuts import render
def index (request):
return render(request, 'app/index.html')
If your CSS and JS files are static don't use Django to serve them, or serve them as static files
For your html you could do the same if it is just some fixed file that won't have any dynamic content. You could also use generic views with the TemplateView, just add a line like this to your urls.py
:
url(r'^path/to/url', TemplateView.as_view(template_name='index.html')),