Django框架班2.5

四.模板系统

1.模板路径:

1.到crm目录下的settings.py文件里找到"TEMPLATES"

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

2.找到模板路径设置 'DIRS': [ ],

3.一般把模板放在项目根目录下,创建一个templates文件夹

4.在'DIRS': [ ],中写上模板路径:'DIRS': [os.path.join(BASE_DIR,'templates') ],

5.在templates文件夹中新建一个teacher文件夹

6.在teacher文件夹中新建一个HTML文件,在这个HTML文件中写好模板

2.导入模板:

1.返回到teacher文件夹下的views.py文件

2.输代码:

1 from django.template.loader import get_template
2 
3 
4 
5 def index (request):
6     tp = get_template('teacher/index.html')
7     html = tp.render()
8     return HttpResponse(html)

猜你喜欢

转载自www.cnblogs.com/jellyli/p/10400491.html
2.5
今日推荐