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 imageio读取视频并进行编解码详解

读视频和写视频一直由于编解码的问题给程序员造成很多麻烦。对此进行了一些探索。用Python读取视频有两种主要方法,分别是基于imageio库和OpenCV,其中OpenCV加上ffmpe...

解决pytorch GPU 计算过程中出现内存耗尽的问题

Pytorch GPU运算过程中会出现:“cuda runtime error(2): out of memory”这样的错误。通常,这种错误是由于在循环中使用全局变量当做累加器,且累加...

Python实现的快速排序算法详解

本文实例讲述了Python实现的快速排序算法。分享给大家供大家参考,具体如下: 快速排序基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有...

python实现用类读取文件数据并计算矩形面积

python实现用类读取文件数据并计算矩形面积

1.创建一个类Rectangle,已知a、b求面积,求三角形的面积 2.结合题目一,从题目一文件中读取数据,并采用类的方法,将计算的结果写在另一个文档中。 (1)利用类进行计算一个矩形的...

Python中的测试模块unittest和doctest的使用教程

我要坦白一点。尽管我是一个应用相当广泛的公共域 Python 库的创造者,但在我的模块中引入的单元测试是非常不系统的。实际上,那些测试大部分 是包括在 gnosis.xml.pickle...