django加载本地html的方法

yipeiwu_com5年前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使用Dijkstra算法实现求解图中最短路径距离问题详解

本文实例讲述了Python使用Dijkstra算法实现求解图中最短路径距离问题。分享给大家供大家参考,具体如下: 这里继续前面一篇《Python基于Floyd算法求解最短路径距离问题》的...

python使用PyGame播放Midi和Mp3文件的方法

本文实例讲述了python使用PyGame播放Midi和Mp3文件的方法。分享给大家供大家参考。具体实现方法如下: ''' pg_midi_sound101.py play midi...

Python复制目录结构脚本代码分享

引言   有个需要,需要把某个目录下的目录结构进行复制,不要文件,当目录结构很少的时候可以手工去建立,当目录结构复杂,目录层次很深,目录很多的时候,这个时候要是还是手动去建立的话,实在不...

django小技巧之html模板中调用对象属性或对象的方法

django小技巧之html模板中调用对象属性或对象的方法

环境:依赖最初test2数据库            python3版本  ...

python创建和使用字典实例详解

字典是python中唯一内建的映射类型。字典中的值并没有特殊的顺序,但是都存储在一个特定的键(key)里。键可以是数字,字符串甚至是元组。1. 创建和使用字典字典可以通过下面的方式创建:...