Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a static HTML file as a response in Django?

I have not figured out how I can present a website with pure HTML code and/or HTML + JavaScript + CSS.

I tried to load an HTML file that just says: Hello World.

I know I can do that with Django too, but later on, I want to display my website with CSS+JavaScript+HTML.

In the views file I run this code:

# Create your views here. from django.http import HttpResponse from django.template import Context, loader  def index(request):     template = loader.get_template("app/index.html")     return HttpResponse(template.render) 

But the only thing the website displays is:

like image 944
jjuser19jj Avatar asked Jan 18 '13 13:01

jjuser19jj


People also ask

What is return render in Django?

In Django, render() is one of the most used functions that combines a template with a context dictionary and returns an HttpResponse object with the rendered text.

What is {% include %} in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.


1 Answers

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')  
like image 174
César García Tapia Avatar answered Sep 25 '22 23:09

César García Tapia