- 01 Django 模板的使用流程
- 1、配置`settings.py`文件。
- 2、定义视图,传递给模板。
- 3、配置子路由
- 4、配置网页
- 5、实现模板功能:`python manage.py runserver`,输入网址:`127.0.0.1:8000/show2Temp/`
- 02 使用模板来实现页面显示功能总结
在工程的film_manager目录下创建:templats、film、index.html.
'DIRS': [os.path.join(base_DIR,'templates')],2、定义视图,传递给模板。
ef show2Templates(requset):
"""
xian shi dao tenmplates
:param requset:
:return:
"""
data = {'title':'Hello,welcom to Django 3.1'}
return render(requset,'film/index.html',data)
3、配置子路由
from django.urls import path,re_path
from film import views
urlpatterns=[
re_path('^show/$',views.show),
re_path('^show2Temp/$',views.show2Templates)
]
4、配置网页
Title
{{title}}
5、实现模板功能:python manage.py runserver,输入网址:127.0.0.1:8000/show2Temp/
02 使用模板来实现页面显示功能总结
1、在应用film_manager同级目录下创建templates/film/index.html/
2、告诉Django 该模板的路径。编辑settings.py文件,即settings.py/TEMLATES/
EMPLATES = [
{
'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、编辑视图函数:film/views.py将需要的数据传递给模板,模板处理完后响应给客户。
def show2Templates(requset):
"""
xian shi dao tenmplates
:param requset:
:return:
"""
data = {'title':'Hello,welcom to Django 3.1'}
return render(requset,'film/index.html',data)
4、模板templates的处理路径:需要去配置路由,film/urls.py
from django.urls import path,re_path
from film import views
urlpatterns=[
re_path('^show/$',views.show),
re_path('^show2Temp/$',views.show2Templates)
]
5、运行项目:python mange.py runserver



