zoukankan      html  css  js  c++  java
  • Django入门模板路径配置及渲染

    模板就是前端的页面,Django把html源码写到模板文件中,然后通过特定方法渲染后交给客户端。
    模板路径设置方法有两种,分别是在项目目录下设置以及在应用目录下设置。
    模板查找顺序:优先在DIRS设置的目录下查找templates,如果没有并且 'APP_DIRS': True时,继续在注册的app文件下查找templates。

    1.在项目目录下设置

    1).在项目目录下新建templates文件夹
    2).在项目目录下的setting.py中找到模板设置TEMPLATES,并进行配置"DIRS"
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')], 
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    
    3).在templates文件夹下创建teacher文件夹
    4).在templates/teacher文件夹下新建teacher_login.html文件,并编辑网页
    <body>
        <h1 style="color:red">我是teacher应用的登录页面</h1>
        <form>
            <p>账号:<input type="text"></p>
            <p>密码:<input type="password"></p>
            <p><input type="submit" value="登录"></p>
        </form>
    </body>
    
    5).在teacher应用文件夹编辑views.py,并使用render进行渲染
    
    # 方法一
    from django.template.loader import get_template
    from django.http import HttpResponse
    
    def login(request):
        tp=get_template('teacher/teacher_login.html')        #获取新建的html文件
        html=tp.render()                                     #网页渲染,html为字符串
        return HttpResponse(html)
    
    #方法二:快捷方式
    from django.shortcuts import render
    
    def login(request)
        return render(request, 'teacher/teacher_login.html')
    
    6).在teacher应用文件夹编辑urls.py
    from django.urls import path
    from . import views
    
    urlpattern=[
        path('login/', views.login)
    ]
    

    2.在应用目录下设置

    1)在student应用目录下新建templates文件夹
    2)在项目目录的setting.py中找到模板设置TEMPLATES和INSTALLED_APP,并上传
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'student'                                       #注册应用
    ]
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')], 
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    
    3).在student/templates文件夹下新建student文件夹
    4).在student/templates/student文件夹下新建student_login.html文件,并编辑网页
    <body>
        <h1 style="color:red">我是student应用的登录页面</h1>
        <form>
            <p>账号:<input type="text"></p>
            <p>密码:<input type="password"></p>
            <p><input type="submit" value="登录"></p>
        </form>
    </body>
    
    5).在student应用文件夹编辑views.py,并使用render进行渲染
    from django.shortcuts import render
    
    def login(request)
        return render(request, 'student/student_login.html')
    
    6).在student应用文件夹编辑urls.py
    from django.urls import path
    from . import views
    
    urlpattern=[
        path('login/', views.login)
    ]
    
  • 相关阅读:
    Linux中带颜色输出的printf使用简介(33)
    Windows中检测当前是否有窗口全屏
    duilib WindowImplBase BUG修复 --- 按一次ESC键, 关闭多个窗口
    【转】OSI七层模型与TCP/IP五层模型
    linux修改文件所有者和文件所在组
    【转】开发人员该如何应对线上故障
    【转】jstack命令的使用
    【转】【JVM】jmap命令详解----查看JVM内存使用详情
    【转】【JVM】jstat命令详解---JVM的统计监测工具
    阿里开源java诊断工具Arthas
  • 原文地址:https://www.cnblogs.com/rongzhen/p/10404365.html
Copyright © 2011-2022 走看看