20170901 django流程之模板, model以及实例

20170901 django流程之model以及实例

.wiz-code-container { position: relative; padding: 8px 0; margin: 5px 25px 5px 5px; text-indent: 0 }
.CodeMirror { font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: black; font-size: 0.83rem }
.CodeMirror-lines { padding: 4px 0 }
.CodeMirror pre { padding: 0 4px }
.CodeMirror-scrollbar-filler,.CodeMirror-gutter-filler { background-color: white }
.CodeMirror-gutters { border-right: 1px solid #ddd; background-color: #f7f7f7; white-space: nowrap }
.CodeMirror-linenumbers { }
.CodeMirror-linenumber { padding: 0 3px 0 5px; min-width: 20px; text-align: right; color: #999; white-space: nowrap }
.CodeMirror-guttermarker { color: black }
.CodeMirror-guttermarker-subtle { color: #999 }
.CodeMirror-cursor { border-left: 1px solid black; border-right: none; width: 0 }
.CodeMirror div.CodeMirror-secondarycursor { border-left: 1px solid silver }
.cm-fat-cursor .CodeMirror-cursor { width: auto; border: 0 !important; background: #7e7 }
.cm-fat-cursor div.CodeMirror-cursors { z-index: 1 }
.cm-animate-fat-cursor { width: auto; border: 0; background-color: #7e7 }
.CodeMirror-overwrite .CodeMirror-cursor { }
.cm-tab { display: inline-block; text-decoration: inherit }
.CodeMirror-rulers { position: absolute; left: 0; right: 0; top: -50px; bottom: -20px; overflow: hidden }
.CodeMirror-ruler { border-left: 1px solid #ccc; top: 0; bottom: 0; position: absolute }
.cm-s-default .cm-header { color: blue }
.cm-s-default .cm-quote { color: #090 }
.cm-negative { color: #d44 }
.cm-positive { color: #292 }
.cm-header,.cm-strong { font-weight: bold }
.cm-em { font-style: italic }
.cm-link { text-decoration: underline }
.cm-strikethrough { text-decoration: line-through }
.cm-s-default .cm-keyword { color: #708 }
.cm-s-default .cm-atom { color: #219 }
.cm-s-default .cm-number { color: #164 }
.cm-s-default .cm-def { color: #00f }
.cm-s-default .cm-variable,.cm-s-default .cm-punctuation,.cm-s-default .cm-property,.cm-s-default .cm-operator { }
.cm-s-default .cm-variable-2 { color: #05a }
.cm-s-default .cm-variable-3 { color: #085 }
.cm-s-default .cm-comment { color: #a50 }
.cm-s-default .cm-string { color: #a11 }
.cm-s-default .cm-string-2 { color: #f50 }
.cm-s-default .cm-meta { color: #555 }
.cm-s-default .cm-qualifier { color: #555 }
.cm-s-default .cm-builtin { color: #30a }
.cm-s-default .cm-bracket { color: #997 }
.cm-s-default .cm-tag { color: #170 }
.cm-s-default .cm-attribute { color: #00c }
.cm-s-default .cm-hr { color: #999 }
.cm-s-default .cm-link { color: #00c }
.cm-s-default .cm-error { color: #f00 }
.cm-invalidchar { color: #f00 }
.CodeMirror-composing { border-bottom: 2px solid }
div.CodeMirror span.CodeMirror-matchingbracket { color: #0f0 }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: #f22 }
.CodeMirror-matchingtag { background: rgba(255, 150, 0, .3) }
.CodeMirror-activeline-background { background: #e8f2ff }
.CodeMirror { position: relative; background: #f5f5f5 }
.CodeMirror-scroll { overflow: hidden !important; margin-bottom: 0; margin-right: 0; padding: 10px 0 16px; outline: none; position: relative }
.CodeMirror-sizer { position: relative; border-right: 30px solid transparent }
.CodeMirror-vscrollbar,.CodeMirror-hscrollbar,.CodeMirror-scrollbar-filler,.CodeMirror-gutter-filler { position: absolute; z-index: 6; display: none }
.CodeMirror-vscrollbar { right: 0; top: 0 }
.CodeMirror-hscrollbar { bottom: 0; left: 0 !important }
.CodeMirror-scrollbar-filler { right: 0; bottom: 0 }
.CodeMirror-gutter-filler { left: 0; bottom: 0 }
.CodeMirror-gutters { position: absolute; left: 0; top: -5px; min-height: 100%; z-index: 3 }
.CodeMirror-gutter { white-space: normal; height: inherit; display: inline-block; vertical-align: top; margin-bottom: -30px }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: none !important; border: none !important }
.CodeMirror-gutter-background { position: absolute; top: 0; bottom: 0; z-index: 4 }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; text-align: center }
.CodeMirror-gutter-wrapper ::selection { background-color: transparent }
.CodeMirror-gutter-wrapper ::-moz-selection { background-color: transparent }
.CodeMirror-lines { cursor: text; min-height: 1px }
.CodeMirror pre { border-width: 0; background: transparent; font-family: inherit; font-size: inherit; margin: 0; white-space: pre; line-height: inherit; color: inherit; z-index: 2; position: relative; overflow: visible }
.CodeMirror-wrap pre { white-space: pre-wrap }
.CodeMirror-linebackground { position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: 0 }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto }
.CodeMirror-widget { }
.CodeMirror-rtl pre { direction: rtl }
.CodeMirror-code { outline: none }
.CodeMirror-scroll,.CodeMirror-sizer,.CodeMirror-gutter,.CodeMirror-gutters,.CodeMirror-linenumber { }
.CodeMirror-measure { position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden }
.CodeMirror-cursor { position: absolute }
.CodeMirror-measure pre { position: static }
div.CodeMirror-cursors { visibility: hidden; position: relative; z-index: 3 }
div.CodeMirror-dragcursors { visibility: visible }
.CodeMirror-focused div.CodeMirror-cursors { visibility: visible }
.CodeMirror-selected { background: #d9d9d9 }
.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0 }
.CodeMirror-crosshair { cursor: crosshair }
.CodeMirror-line::selection,.CodeMirror-line>span::selection,.CodeMirror-line>span>span::selection { background: #d7d4f0 }
.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0 }
.cm-searching { background: rgba(255, 255, 0, .4) }
.cm-force-border { padding-right: .1px }
.cm-tab-wrap-hack::after { content: "" }
span.CodeMirror-selectedtext { background: none }
.CodeMirror-activeline-background,.CodeMirror-selected { }
.CodeMirror-blur .CodeMirror-activeline-background,.CodeMirror-blur .CodeMirror-selected { visibility: hidden }
.CodeMirror-blur .CodeMirror-matchingbracket { color: inherit !important; outline: none !important; text-decoration: none !important }
.cm-s-blackboard.CodeMirror { background: #0C1021; color: #F8F8F8 }
.cm-s-blackboard div.CodeMirror-selected { background: #253B76 }
.cm-s-blackboard .CodeMirror-line::selection,.cm-s-blackboard .CodeMirror-line>span::selection,.cm-s-blackboard .CodeMirror-line>span>span::selection { background: rgba(37, 59, 118, .99) }
.cm-s-blackboard .CodeMirror-line::-moz-selection, .cm-s-blackboard .CodeMirror-line > span::-moz-selection, .cm-s-blackboard .CodeMirror-line > span > span::-moz-selection { background: rgba(37, 59, 118, .99) }
.cm-s-blackboard .CodeMirror-gutters { background: #0C1021; border-right: 0 }
.cm-s-blackboard .CodeMirror-guttermarker { color: #FBDE2D }
.cm-s-blackboard .CodeMirror-guttermarker-subtle { color: #888 }
.cm-s-blackboard .CodeMirror-linenumber { color: #888 }
.cm-s-blackboard .CodeMirror-cursor { border-left: 1px solid #A7A7A7 }
.cm-s-blackboard .cm-keyword { color: #FBDE2D }
.cm-s-blackboard .cm-atom { color: #D8FA3C }
.cm-s-blackboard .cm-number { color: #D8FA3C }
.cm-s-blackboard .cm-def { color: #8DA6CE }
.cm-s-blackboard .cm-variable { color: #FF6400 }
.cm-s-blackboard .cm-operator { color: #FBDE2D }
.cm-s-blackboard .cm-comment { color: #AEAEAE }
.cm-s-blackboard .cm-string { color: #61CE3C }
.cm-s-blackboard .cm-string-2 { color: #61CE3C }
.cm-s-blackboard .cm-meta { color: #D8FA3C }
.cm-s-blackboard .cm-builtin { color: #8DA6CE }
.cm-s-blackboard .cm-tag { color: #8DA6CE }
.cm-s-blackboard .cm-attribute { color: #8DA6CE }
.cm-s-blackboard .cm-header { color: #FF6400 }
.cm-s-blackboard .cm-hr { color: #AEAEAE }
.cm-s-blackboard .cm-link { color: #8DA6CE }
.cm-s-blackboard .cm-error { background: #9D1E15; color: #F8F8F8 }
.cm-s-blackboard .CodeMirror-activeline-background { background: #3C3636 }
.cm-s-blackboard .CodeMirror-matchingbracket { outline: 1px solid grey; color: white !important }
html,body { font-size: 12pt }
body { font-family: Helvetica, "Hiragino Sans GB", "微软雅黑", "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; margin: 0 auto; padding: 1.25rem 1rem }
h1,h2,h3,h4,h5,h6 { margin: 1.25rem 0 0.625rem; padding: 0; font-weight: bold }
h1 { font-size: 1.67rem }
h2 { font-size: 1.5rem }
h3 { font-size: 1.25rem }
h4 { font-size: 1.17rem }
h5 { font-size: 1rem }
h6 { font-size: 1rem; color: #777777; margin: 1rem 0 }
div,p,ul,ol,dl,li { margin: 0 }
blockquote,table,pre,code { margin: 8px 0 }
ul,ol { padding-left: 2rem }
ol.wiz-list-level1>li { list-style-type: decimal }
ol.wiz-list-level2>li { list-style-type: lower-latin }
ol.wiz-list-level3>li { list-style-type: lower-roman }
blockquote { padding: 0 0.75rem }
blockquote>:first-child { margin-top: 0 }
blockquote>:last-child { margin-bottom: 0 }
img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0 }
table { border-collapse: collapse; border: 1px solid #bbbbbb }
td,th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb; min-height: 28px }
.wiz-hide { display: none !important }

<4>生成同步数据库的脚本:python manage.py makemigrations

同步数据库: python manage.py migrate

注意:在开发过程中,数据库同步误操作之后,难免会遇到后面不能同步成功的情况,解决这个问题的一个简单粗暴方法是把migrations目录下

的脚本(除__init__.py之外)全部删掉,再把数据库删掉之后创建一个新的数据库,数据库同步操作再重新做一遍。

<5>当我们访问http://127.0.0.1:8080/admin/时,会出现:

所以我们需要为进入这个项目的后台创建超级管理员:python manage.py createsuperuser设置好用户名和密码后便可登录啦!

<6>清空数据库:python manage.py flush

<7>查询某个命令的详细信息: django-admin.py help startapp

admin 是Django 自带的一个后台数据库管理系统。

<8>启动交互界面 :python manage.py shell

这个命令和直接运行 python 进入 shell 的区别是:你可以在这个 shell 里面调用当前项目的 models.py 中的 API,对于操作数据,还有一些小测试非常方便。

<9>终端上输入python manage.py 可以看到详细的列表,在忘记子名称的时候特别有用

实例练习1-提交数据并展示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>创建个人信息</h1>

<form action="/userInfor/" method="post">

<p>姓名<input type="text" name="username"></p>
<p>性别<input type="text" name="sex"></p>
<p>邮箱<input type="text" name="email"></p>
<p><input type="submit" value="submit"></p>

</form>

<hr>

<h1>信息展示</h1>

<table border="1">

<tr>
<td>姓名</td>
<td>性别</td>
<td>邮箱</td>
</tr>
{% for i in info_list %}

<tr>
<td>{{ i.username }}</td>
<td>{{ i.sex }}</td>
<td>{{ i.email }}</td>
</tr>

{% endfor %}

</table>

</body>
</html>

-----------------------url.py---------------------------------------
url(r‘^userInfor/‘, views.userInfor)

-----------------------views.py--------------------------------------

info_list=[]

def userInfor(req):

if req.method=="POST":
username=req.POST.get("username",None)
sex=req.POST.get("sex",None)
email=req.POST.get("email",None)

info={"username":username,"sex":sex,"email":email}
info_list.append(info)

return render(req,"userInfor.html",{"info_list":info_list})<wiz_code_mirror>

xxxxxxxxxx

63

63

1

<!DOCTYPE html>

2

<html lang="en">

3

<head>

4

    <meta charset="UTF-8">

5

    <title>Title</title>

6

</head>

7

<body>

8

<h1>创建个人信息</h1>

9


10

<form action="/userInfor/" method="post">

11


12

    <p>姓名<input type="text" name="username"></p>

13

    <p>性别<input type="text" name="sex"></p>

14

    <p>邮箱<input type="text" name="email"></p>

15

    <p><input type="submit" value="submit"></p>

16


17

</form>

18


19

<hr>

20


21

<h1>信息展示</h1>

22


23

<table border="1">

24


25

    <tr>

26

        <td>姓名</td>

27

        <td>性别</td>

28

        <td>邮箱</td>

29

    </tr>

30

    {% for i in info_list %}

31


32

        <tr>

33

            <td>{{ i.username }}</td>

34

            <td>{{ i.sex }}</td>

35

            <td>{{ i.email }}</td>

36

        </tr>

37


38

    {% endfor %}

39


40

</table>

41


42

</body>

43

</html>

44


45


46

-----------------------url.py---------------------------------------

47

url(r‘^userInfor/‘, views.userInfor)

48


49

-----------------------views.py--------------------------------------

50


51

info_list=[]

52


53

def userInfor(req):

54


55

    if req.method=="POST":

56

        username=req.POST.get("username",None)

57

        sex=req.POST.get("sex",None)

58

        email=req.POST.get("email",None)

59


60

        info={"username":username,"sex":sex,"email":email}

61

        info_list.append(info)

62


63

    return render(req,"userInfor.html",{"info_list":info_list})

实例练习2-提交数据并展示(数据库)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>创建个人信息</h1>

<form action="/userInfor/" method="post">

<p>姓名<input type="text" name="username"></p>
<p>性别<input type="text" name="sex"></p>
<p>邮箱<input type="text" name="email"></p>
<p><input type="submit" value="submit"></p>

</form>

<hr>

<h1>信息展示</h1>

<table border="1">

<tr>
<td>姓名</td>
<td>性别</td>
<td>邮箱</td>
</tr>
{% for i in info_list %}

<tr>
<td>{{ i.username }}</td>
<td>{{ i.sex }}</td>
<td>{{ i.email }}</td>
</tr>

{% endfor %}

</table>

</body>
</html>

----------------------------------------------models.py
from django.db import models

# Create your models here.

class UserInfor(models.Model):

username=models.CharField(max_length=64)
sex=models.CharField(max_length=64)
email=models.CharField(max_length=64)

----------------------------------------------views.py

from django.shortcuts import render

from app01 import models
# Create your views here.

def userInfor(req):

if req.method=="POST":
u=req.POST.get("username",None)
s=req.POST.get("sex",None)
e=req.POST.get("email",None)

#---------表中插入数据方式一
# info={"username":u,"sex":e,"email":e}
# models.UserInfor.objects.create(**info)

#---------表中插入数据方式二
models.UserInfor.objects.create(
username=u,
sex=s,
email=e
)

info_list=models.UserInfor.objects.all()

return render(req,"userInfor.html",{"info_list":info_list})

return render(req,"userInfor.html")<wiz_code_mirror>

x

89

1

<!DOCTYPE html>

2

<html lang="en">

3

<head>

4

    <meta charset="UTF-8">

5

    <title>Title</title>

6

</head>

7

<body>

8

<h1>创建个人信息</h1>

9


10

<form action="/userInfor/" method="post">

11


12

    <p>姓名<input type="text" name="username"></p>

13

    <p>性别<input type="text" name="sex"></p>

14

    <p>邮箱<input type="text" name="email"></p>

15

    <p><input type="submit" value="submit"></p>

16


17

</form>

18


19

<hr>

20


21

<h1>信息展示</h1>

22


23

<table border="1">

24


25

    <tr>

26

        <td>姓名</td>

27

        <td>性别</td>

28

        <td>邮箱</td>

29

    </tr>

30

    {% for i in info_list %}

31


32

        <tr>

33

            <td>{{ i.username }}</td>

34

            <td>{{ i.sex }}</td>

35

            <td>{{ i.email }}</td>

36

        </tr>

37


38

    {% endfor %}

39


40

</table>

41


42

</body>

43

</html>

44


45


46

----------------------------------------------models.py

47

from django.db import models

48


49

# Create your models here.

50


51


52

class UserInfor(models.Model):

53


54

    username=models.CharField(max_length=64)

55

    sex=models.CharField(max_length=64)

56

    email=models.CharField(max_length=64)

57


58

----------------------------------------------views.py

59


60

from django.shortcuts import render

61


62

from app01 import models

63

# Create your views here.

64


65


66

def userInfor(req):

67


68

    if req.method=="POST":

69

        u=req.POST.get("username",None)

70

        s=req.POST.get("sex",None)

71

        e=req.POST.get("email",None)

72


73


74

       #---------表中插入数据方式一

75

            # info={"username":u,"sex":e,"email":e}

76

            # models.UserInfor.objects.create(**info)

77


78

       #---------表中插入数据方式二

79

        models.UserInfor.objects.create(

80

            username=u,

81

            sex=s,

82

            email=e

83

        )

84


85

        info_list=models.UserInfor.objects.all()

86


87

        return render(req,"userInfor.html",{"info_list":info_list})

88


89

    return render(req,"userInfor.html")
时间: 2024-11-05 14:48:38

20170901 django流程之模板, model以及实例的相关文章

Django流程-以登录功能为例

Django流程-以登录功能为例 一.注意点 1.新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2.启动Django项目的时候,一定要确保一个端口号只有一个Django项目占用,不然的话,会容易造成bug(修改代码后刷新页面没有效果) 3.用户可以访问的资源,都在url中,只有url中开设相关的资源你才能访问到 4.后端资源一般都需要手动指定是否需要暴露给用户 5.Django是默认支持自动重启代码吗的,所以你只

python Django教程 之模板渲染、循环、条件判断、常用的标签、过滤器

python3.5 manage.py runserver python Django教程 之模板渲染.循环.条件判断.常用的标签.过滤器 一.Django模板渲染模板 注: 本节代码是基于 Django 1.8,但 Django 1.4 - Djagno 1.9 操作流程也是一样的. 后文给出示例代码可以在 Django 1.4.x 到 Django 1.9.x 上运行. 不过还是强调一点,一点要动手敲代码!不要偷懒哦,动手才能学到真东西. 1. 创建一个 zqxt_tmpl 项目,和一个 名

Django基础核心技术之Model模型的介绍与设计

Django基础核心技术之Model模型的介绍与设计原创: Yunbo Shi Python Web与Django开发 2018-05-03Django网络应用开发的5项基础核心技术包括模型(Model)的设计,URL的配置,View(视图)的编写,Template(模板)的设计和Form(表单)的使用.今天小编我就拼了老命来用千字长文给你来介绍下第一项Django核心基础知识之Model的设计吧.想持续了解后续Django Web开发技术请订阅我的公众号[Python与Django大咖之路].

django之定义模板语法08({{ string | truncatewords:&#39;2&#39; }})

#index页面代码: <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>过滤器</h1> <p>{{ string | truncatewords:'2' }}</p> //显示前2个字(非字母) </body

Python笔记(五)--Django中使用模板

使用Django开发网站时,如果不使用模板,那么将会很不合理.因为我们所有的html代码都需要被硬编码到我们的Python代码中.我们新建一个工程,然后再在新建一个应用程序,并在其中的views.py文件中添加如下代码: 1 #coding=UTF-8 2 from django.http.response import HttpResponse, Http404 3 import datetime 4 5 def hours_ahead(request,offset): 6 try: 7 of

django学习之模板系统

django模板系统学习笔记之for 在for标签里面,有以下知识点: forloop.counter forloop.counter0 forloop.first forloop.last forloop.revcounter forloop.revcounter0 froloop.parentloop empty 下面我们举例学习: 首先到我们的工程目录下,敲入: python manage.py shell # forloop.counter from django.template im

利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理

利用反射和泛型把Model对象按行储存进数据库以及按行取出然后转换成Model 类实例 MVC网站通用配置项管理 2018-3-10 15:18 | 发布:Admin | 分类:代码库 | 评论:0 | 浏览:45 | 该类在MVC中可以方便管理配置信息,可以直接把Model储存进数据库或者从数据库去除数据转为Model. 1 何为配置项目? 比如网站的名称,页脚信息,meta中的KeyWord信息等,如果不想硬编码到网页里,就需要使用配置文件进行储存,通常都是储存到数据库中.使用的时候读取出来

Django框架之模板

Django框架之模板层 什么是模板 需要返回给浏览器的html文件 Django的模板 = HTML代码 + 模板语言 模板语言 有与我们直接返回html页面的话,那他只是一个静态页面无法实现前后端数据交互,那么我们就需要经过页面渲染,让静态文件呈现出动态页面的效果. 模板语言的变量 如果我们需要将后端的数据传递到前端页面渲染,就需要用到模板语言中的变量 用法:通过{{}}来获取后端传递过来的数据 变量支持的数据类型: int float str dict list tuple set boo

Django REST framework之解析器实例以及源码流程分析

解析器 我们都知道源生Django默认只能解析content-type:application/x-www-form-urlencoded头格式的,若果是json格式,form-data格式都要自己处理. 但是在restframework已经为我们做好了,只要配置上,就能帮我们解析请求的数据 举例说明: 表设计: 1 from django.db import models 2 3 4 class UserGroup(models.Model): 5 title = models.CharFie