Display simple DJango html pages

I am currently using Django 1.5 and cannot figure out how to render a simple html page. I read about class based views, but not sure if this is what I want to do.

I am trying to display a simple index.html page, but according to some examples I saw, I need to put this code in app / views.py:

    def index(request):
        template = loader.get_template("app/index.html")
        return HttpResponse(template.render)

Is there a reason my index.html page should be related to the application related to my django project? It seems to me more appropriate for the index.html page to fit the overall project.

In more detail, using this code in the views.py file, what do I need to insert in my urls.py so that index.html is actually?

EDIT:

Django project structure:

webapp/
    myapp/
        __init__.py
        models.py
        tests.py
        views.py
    manage.py
    project/
        __init__.py
        settings.py
        templates/
            index.html
        urls.py
        wsgi.py
+5
source share
3

urls.py

from django.conf.urls import patterns, url
from app_name.views import *

urlpatterns = patterns('',
    url(r'^$', IndexView.as_view()),
)

views.py

from django.views.generic import TemplateView

class IndexView(TemplateView):
    template_name = 'index.html'

@Ezequiel Bertti app

from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^index.html', TemplateView.as_view(template_name="index.html")),
)

index.html

webapp/
    myapp/
        __init__.py
        models.py
        tests.py
        views.py
        templates/      <---add
            index.html  <---add
    manage.py
    project/
        __init__.py
        settings.py
        templates/      <---remove
            index.html  <---remove
        urls.py
        wsgi.py
+7

GenericView django:

from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^index.html', TemplateView.as_view(template_name="index.html")),
)
+4

Make sure you configure the path in the settings.py file. This will probably solve the TemplateDoesNoteExist error.

You can put below one in settings.py.

SETTINGS_PATH = os.path.normpath (os.path.dirname ( file ))

TEMPLATE_DIRS = (os.path.join (SETTINGS_PATH, 'templates'),)

0
source

All Articles