django加载本地html的方法

yipeiwu_com6年前Python基础

django加载本地html

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render,render_to_response
# Create your views here.
def hello(request):
 return render_to_response("hello.html")

传递数据到html中

python代码

# Create your views here.
# http://weibo.com/lixiaodaoaaa
class Person(object):
 def __init__(self, name, age, sex):
  self.name = name
  self.age = age
  self.sex = sex
 def say(self):
  return self.name
def hello(request):
 u_user = Person("dog", 18, "male")
 myList = ["sendList to the html files", "god", "god02"]
 u_content_dic = {"u_title": "Title Is Here", "u_user": u_user,"u_test_str":myList}
 ##传递一个字典作为Content_type
 return render_to_response("hello.html", u_content_dic)

Html代码去取值:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title>{{ u_title }}</title>
 <h1>{{ u_user.age }}</h1>
 <h1>{{ u_user.name }}</h1>
 <h1>{{ u_user.sex}}</h1>
 <h1>{{ u_test_str.0}}</h1>
 <h1>{{ u_test_str.1}}</h1>
 <br/>
 <h1>{{ u_user.say}}</h1>
</head>
<body>
</body>
</html>

以上这篇django加载本地html的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用pandas对数据进行差分运算的方法

如下所示: >>> import pandas as pd >>> import numpy as np # 生成模拟数据 >>&g...

python机器学习理论与实战(一)K近邻法

python机器学习理论与实战(一)K近邻法

机器学习分两大类,有监督学习(supervised learning)和无监督学习(unsupervised learning)。有监督学习又可分两类:分类(classification...

详解python之简单主机批量管理工具

详解python之简单主机批量管理工具

今天做了一个很简单的小项目,感受到了paramiko模块的强大,也深感自己Linux的功力不行~~ 一、需求 二、简单需求分析及流程图 需求很少,我就简单地说下: 1. 主机分...

Python import与from import使用及区别介绍

下面介绍下Python import与from import使用,具体内容如下所示: Python程序可以调用一组基本的函数(即内建函数),比如print()、input()和len()...

python3操作微信itchat实现发送图片

python3操作微信itchat实现发送图片

两个代码,一个是把python当微信操作,可以查看自己的好友信息,群信息等。还可以定时发送文本,文件等。效果就和你本人操作一样,没差别。 还有一个是连接你的微信和图灵机器人,别人和你说话...