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:
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.
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.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With