显示简单的HTML页面DJango

前端之家收集整理的这篇文章主要介绍了显示简单的HTML页面DJango前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我目前正在使用Django 1.5,无法弄清楚如何显示一个简单的html页面.我一直在阅读有关基于类的视图,但我不确定这是我想要做的.

我正在尝试显示一个简单的index.html页面,但根据一些示例,我看到我需要将此代码放在app / views.py中:

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

我的index.html页面是否必须与我的django项目相关联的应用程序相关联?对我来说,让index.html页面与整个项目相对应似乎更有意义.

更重要的是,在我的views.py文件中使用此代码,我需要在urls.py中添加什么才能真正实现index.html?

编辑:

Django项目的结构:

  1. webapp/
  2. myapp/
  3. __init__.py
  4. models.py
  5. tests.py
  6. views.py
  7. manage.py
  8. project/
  9. __init__.py
  10. settings.py
  11. templates/
  12. index.html
  13. urls.py
  14. wsgi.py
最佳答案
urls.py

  1. from django.conf.urls import patterns,url
  2. from app_name.views import *
  3. urlpatterns = patterns('',url(r'^$',IndexView.as_view()),)

views.py

  1. from django.views.generic import TemplateView
  2. class IndexView(TemplateView):
  3. template_name = 'index.html'

根据@Ezequiel Bertti的回答,删除应用

  1. from django.conf.urls import patterns
  2. from django.views.generic import TemplateView
  3. urlpatterns = patterns('',(r'^index.html',TemplateView.as_view(template_name="index.html")),)

您的index.html必须存储在模板文件夹中

  1. webapp/
  2. myapp/
  3. __init__.py
  4. models.py
  5. tests.py
  6. views.py
  7. templates/ <---add
  8. index.html <---add
  9. manage.py
  10. project/
  11. __init__.py
  12. settings.py
  13. templates/ <---remove
  14. index.html <---remove
  15. urls.py
  16. wsgi.py

猜你在找的HTML相关文章