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
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
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")),
)
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