博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下开发python django程序(模板设置和载入数据)
阅读量:4646 次
发布时间:2019-06-09

本文共 1251 字,大约阅读时间需要 4 分钟。

1.添加templates文件夹

2.修改settings.py文件

import os #引用 os模块BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  #添加BASE_DIR路径TEMPLATE_DIRS = (    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".    # Always use forward slashes, even on Windows.    # Don't forget to use absolute paths, not relative paths.    os.path.join(BASE_DIR,  '\\csvt03\\templates').replace('\\','/'),    'app1',) #在TEMPLATE_DIRS节点中设置模板文件夹路径

 

2.views 3种方式 载入模板

from django.http import HttpResponsefrom django.template import loader,Context,Templatefrom django.shortcuts import render_to_responsedef index(req):        t=loader.get_template('index.html')        c = Context({})        return HttpResponse(t.render(c))def index1(req):        user={'name':'wanghao','age':32,'addr':'cq','sex':'Fmale'}        return render_to_response('index.html',{'user':user})def index2(req):        t= Template('

hello {
{user.name}}

age:{
{user.age}}
address:{
{user.addr}}
sex:{
{user.sex}}
') user={'name':'wuxi','age':31,'addr':'cq','sex':'male'} c=Context({'user':user}) return HttpResponse(t.render(c))

 

转载于:https://www.cnblogs.com/whzym111/p/5888602.html

你可能感兴趣的文章
Mac上搭建rtmp流媒体服务器(结合FFmpeg的使用)
查看>>
HTML5⑥
查看>>
将jar包安装到本地仓库
查看>>
2333
查看>>
T4:益智游戏
查看>>
JS概述
查看>>
codeforces 712B Memory and Trident
查看>>
并行编译Parallel Building
查看>>
淘宝处理高并发
查看>>
14、equals 与 == 的区别
查看>>
处理爬虫遇到的乱码问题
查看>>
python---help
查看>>
爱你现在的时光 ---白岩松
查看>>
大话RabbitMQ 基础入门
查看>>
非法字符:"\ufeff"
查看>>
BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
查看>>
音乐收藏
查看>>
设为首页,加入收藏js代码
查看>>
android同时使用多个library时的问题
查看>>
今天升级了ADT到ADT 22.6.1,打包混淆的时候就出现了问题
查看>>