加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

显示简单的HTML页面DJango

发布时间:2020-12-14 23:18:56 所属栏目:资源 来源:网络整理
导读:我目前正在使用Django 1.5,无法弄清楚如何显示一个简单的html页面.我一直在阅读有关基于类的视图,但我不确定这是我想要做的. 我正在尝试显示一个简单的index.html页面,但根据一些示例,我看到我需要将此代码放在app / views.py中: def index(request): templ

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

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

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

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

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

编辑:

Django项目的结构:

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'

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读