django 模板路径设置
新建django项目mysite,mysite文件夹下会有manage.py和文件夹mysite(子文件夹),我的模板文件夹templates和他们俩在一块。要想通过相对路径访问模板,需要设置子文件夹mysite中setting.py
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '../templates').replace('\\','/'),
)
当然,路径的设置是相对于setting.py的,如果templates和setting.py在同一个文件夹,os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),。
模板路径设好以后,在视图中from django.template.loader import get_template
直接t=get_template(模板文件名)就可以获得模板文件
当然可以直接通过from django.shortcuts import render_to_response
return render_to_response(模板文件名,{参数列表}),直接返回HttpResponse