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实现各进制转换的总结大全

前言 玩ctf经常遇到进制转换的问题,就正好做一个进制转换总结,分享出来供大家参考学习,下面来一起看看详细的介绍: 字符串与十六进制转换 例如百度ctf 12月的第二场第一个misc...

python requests 库请求带有文件参数的接口实例

python requests 库请求带有文件参数的接口实例

有些接口参数是一个文件格式,比如fiddler 抓包参数如下显示 这个接口的 form-data fiddler 显示的和不带文件参数的接口有明显区别,显示的不是简单的键值对,所以我们...

Python中optparser库用法实例详解

本文研究的主要是Python中optparser库的相关内容,具体如下。 一直以来对optparser不是特别的理解,今天就狠下心,静下心研究了一下这个库。当然了,不敢说理解的很到位,但...

pytorch-RNN进行回归曲线预测方式

pytorch-RNN进行回归曲线预测方式

任务 通过输入的sin曲线与预测出对应的cos曲线 #初始加载包 和定义参数 import torch from torch import nn import numpy as np...

Python中字符串对齐方法介绍

目的   实现字符串的左对齐,右对齐,居中对齐。 方法   字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符 复制代码...