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)
]
posted @ 2019-02-20 17:30  小雨快停  阅读(1514)  评论(0编辑  收藏  举报