Shawn Taylor
Shawn Taylor

Reputation: 1454

Displaying simple html pages DJango

I'm currently using Django 1.5 and can't figure out how to display a simple html page. I've been reading about class based views, but am not sure this is what I want to do.

I'm trying to display a simple index.html page, but according to some examples I've seen 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 why my index.html page has to be associated with the app associated with my django project? For me, it seems to make more sense to have an index.html page correspond to the overall project.

More to the point, using this code in my views.py file, what do I need to put into my urls.py in order to actually index.html?

EDIT:

Structure of Django project:

webapp/
    myapp/
        __init__.py
        models.py
        tests.py
        views.py
    manage.py
    project/
        __init__.py
        settings.py
        templates/
            index.html
        urls.py
        wsgi.py

Upvotes: 7

Views: 5986

Answers (3)

bhanu
bhanu

Reputation: 86

Make sure that you have configured the path in setting.py. Probably it will resolve TemplateDoesNoteExist error.

You can place the below one in settings.py.

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

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

Upvotes: 0

Ezequiel Bertti
Ezequiel Bertti

Reputation: 812

You can use a GenericView of django core:

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

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

Upvotes: 4

catherine
catherine

Reputation: 22808

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'

Based on @Ezequiel Bertti answer, remove 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")),
)

Your index.html must be store inside templates folder

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

Upvotes: 11

Related Questions