33

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

I tried to load a HTML file with 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:

>

Mateusz Piotrowski
  • 6,087
  • 9
  • 44
  • 71
jjuser19jj
  • 1,303
  • 2
  • 15
  • 34
  • 2
    In production you'll want to avoid using Django to serve static assets. Also, take a look at https://docs.djangoproject.com/en/1.8/howto/static-files/ . If the code you posted is an intermediate step you are taking prior to adding dynamic behavior that's one thing - but if app/index.html is truly a static html page than you should serve it as a static asset in my opinion. – chucksmash Apr 21 '15 at 14:13

6 Answers6

31

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')

César García Tapia
  • 2,988
  • 3
  • 26
  • 47
  • When I do this, the browser just displays the content of the html file which is:

    hello world

    some_text
    – jjuser19jj Jan 18 '13 at 14:06
  • 2
    Since Django 3.0 `render_to_response` shortcut was removed. According to https://stackoverflow.com/a/55911436/7444782 code `return render_to_response('app/index.html')` could be substituted to `return render(None, 'app/index.html')`. – and1er Jan 31 '20 at 10:50
19

You are not calling the render method there, are you?

Compare:

template.render

template.render()
ustun
  • 6,593
  • 5
  • 41
  • 55
  • Ahh yeah right thanks, miss syntax checking... Is there anythin else Is there anythin else I have to pay attention to when I want to use django+html+javascript+css ?? – jjuser19jj Jan 18 '13 at 13:58
  • 1
    Read on the `static` app (builtin) and `webassets` (for js/css minification and cache busting.) – ustun Jan 19 '13 at 14:29
15

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')),
Anonymous
  • 10,357
  • 3
  • 37
  • 49
Facundo Casco
  • 8,729
  • 5
  • 40
  • 61
  • This allowed me to launch the page from django but the text inside {{}} template operator were not appearing.I was using angular js inside the html page this was conflicting with django template {{ operators. I solved the issue by using the recommendation from the post http://stackoverflow.com/questions/8302928/angularjs-with-django-conflicting-template-tags – Vijay Jul 13 '15 at 06:43
  • 1
    do not forget to include `from django.views.generic import TemplateView` in `urls.py` – Timo May 13 '18 at 14:30
13

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')
Mayur Sable
  • 131
  • 1
  • 3
2

First, you need to do some settings for templating:

Create "templates" folder in the root directory, where manage.py is located after you created your project. Then go to settings.py , in the TEMPLATES section enter the directory of templates folder. Hard coding is not recommended because if you send your project to your friend, your friend won't be able to run it. Should be like this:

TEMPLATES = [
 {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Assuming that you have home.html and about.html have in templates folder:

urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/',views.aboutview),
    path('',views.homeview),
]

views.py

from django.http import HttpResponse
from django.shortcuts import render

def aboutview(request):
  return render(request,"home.html")
def homeview(request):
  return render(request,"about.html")
Adrita Sharma
  • 17,967
  • 8
  • 40
  • 61
Yilmaz
  • 4,262
  • 6
  • 24
  • 52
0
from django.http import HttpResponse

from django.template import loader

def index(request):

    template=loader.get_template('app/index.html')

    return HttpResponse(template)
amigana _34
  • 15
  • 1
  • 6
  • 5
    Your code differs from OP's code only by one little detail (`template.render`vs. `template`). You may want to explain why this solves OP's problem. – Adrian W Apr 02 '19 at 09:50