s12-20160507-day16

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

pytho自动化开发 day16

Date:2016.05.07

    @南非波波

课程大纲:

http://www.cnblogs.com/alex3714/articles/5457672.html

一、Django起步

1.创建Django项目

两种方式:使用pycharm工具进行创建
使用manage命令进行创建

2.url设计

3.views视图设计

4.Template版本设计

二、Template模板渲染

1.调用后端传递的数据

<h3>username:{{user_obj.username}}</h3>
<h3>E-mail:{{user_obj.email}}</h3>

2.使用for循环进行遍历数据

<url>
{% for user_info in user_obj %}
<li style="">username:{{user_info.username}},E-mail:{{user_info.email}}</li>
{% endfor %}
</url>

3.使用if进行相关条件判断

<url>
{% for user_info in user_obj %}
{# {% if user_info.username == "swht" %}#} <!--可以直接判断某个字符串,然后进行处理-->
{% if forloop.counter0|divisibleby:"2" %} <!--使用forloop。counter参数获取遍历的当前数字。counter0是从0开始的索引值,divisibleby:"2"是指能够被2整除-->
<li style="background-color: aqua">username:{{user_info.username}},E-mail:{{user_info.email}}</li>
{% else %}
<li style="">username:{{user_info.username}},E-mail:{{user_info.email}}</li>
{% endif %} {% endfor %}
</url>

4.模板的继承与复用

使用主页的头部进行继承,然后将主体进行重写

index.html

{% block header-menu %}
<ul >
<li style="display: inline-block">Home</li>
<li style="display: inline-block">page1</li>
<li style="display: inline-block">page2</li>
<li style="display: inline-block">page3</li>
</ul>
{% endblock %}
{% block content-container %}
<h2>这是文章的首页</h2>
<!--<h3>username:{{user_obj.username}}</h3>-->
<!--<h3>E-mail:{{user_obj.email}}</h3>-->
<url>
{% for user_info in user_obj %}
{# {% if user_info.username == "swht" %}#} <!--可以直接判断某个字符串,然后进行处理-->
{% if forloop.counter0|divisibleby:"2" %} <!--使用forloop。counter参数获取遍历的当前数字。counter0是从0开始的索引值,divisibleby:"2"是指能够被2整除-->
<li style="background-color: aqua">username:{{user_info.username}},E-mail:{{user_info.email}}</li>
{% else %}
<li style="">username:{{user_info.username}},E-mail:{{user_info.email}}</li>
{% endif %} {% endfor %}
</url>
{% endblock %} year.html {% extends 'article/index.html' %}
{% block content-container %}
<h1>这是{{ year_obj.year }}年的文章</h1>
{% endblock %}

说明:

{% extends 'article/index.html' %} 语法是继承主页的所有属性
{% block content-container %}
{% endblock%} 语法说明该语块是可以被重写的

模板的继承和重写支持:子继承父、孙继承子

三、配置文件

1.setting文件说明

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  指定了当前项目文件的基础路径
SECRET_KEY = '-i6p7t+w6@&duki)8s88tf=_ms%@z8fz4_5g@vhvzp@tij36w$' django通信KEY,我们一般不需要进行修改 DEBUG = True #选项为True时,属于开发环境,当遇到错误时会弹出默认的提示大黄页,当选项为False时,属于生产环境,需要下面的关键字进行配合设置
ALLOWED_HOSTS = [] #当DEBUG = False时,ALLOWED_HOSTS = [*],即允许所有主机访问,当访问的资源不存在时,默认会返回默认的404页面提示
INSTALLED_APPS=[] #这里需要配置我们安装的应用的名称
TEMPLATES = [] #这里指定项目模板的路径,一般不需要修改,但也可以自定义路径
DATABASES = {} #这里默认使用sqlite3数据库进行存储,我们可以修改成支持的其他数据库类型
TIME_ZONE = 'Asia/Shanghai' #这里的时区参数需要选择上海时区

2.设置mysql数据库配置

DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'blog',
'HOST':'127.0.0.1',
'PORT':'3306',
'USER':'root',
'PASSWORD':'',
}
} Windows下面安装mysql环境:http://www.cnblogs.com/songqingbo/p/5080847.html 创建所需的数据库:
create database blog charset utf8;

3.mysql数据库初始化

models

class Author(models.Model):
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
email = models.EmailField()
def __unicode__(self):
return "<%s %s>" %(self.first_name,self.last_name) class Meta:
verbose_name_plural = u"作者"
#def __str__()
class Publisher(models.Model):
name = models.CharField(max_length=64,unique=True)
address = models.CharField(max_length=128,null=True,blank=True)
city = models.CharField(max_length=64)
state_province = models.CharField(max_length=30,help_text="put your country code here..",verbose_name=u"所属省")
country = models.CharField(max_length=50,editable=False)
website = models.URLField()
def __unicode__(self):
return "<%s>" %(self.name)
class Book(models.Model):
name = models.CharField(max_length=128)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
#publish_date = models.DateTimeField
publish_date = models.DateField()
def __unicode__(self):
return "<%s>" %(self.name)

由于django需要MySQLdb进行连接mysql数据库,需要安装MySQLdb模块和vc++ for python2.7环境包。

python2 manage.py makemigrations  #生成数据库创建表文件
Migrations for 'article':
0001_initial.py:
- Create model Author
- Create model Book
- Create model Publisher
- Add field publisher to book
python2 manage.py migrate #将表结构写入数据库
Operations to perform:
Apply all migrations: admin, article, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying article.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK

4.创建后台用户

admin.py
#增加下面的语句作用是使用admin后台进行数据库的管理操作
import models
admin.site.register(models.Author)
admin.site.register(models.Book)
admin.site.register(models.Publisher)
python2 manage.py createsuperuser #创建一个超级用户

四、使用django命令对数据库进行基础操作

  1. 进入python django shell

    shell>>python2 manage.py shell

  2. 查询数据

    >>>from article.models import Publisher

    >>>publisher_list = Publisher.objects.all() #查询所有的出版社列表

    >>>publisher_list[1].name #查询第2个出版社的名称

    >>>publisher_list[1].id #查询第2个出版社的id

  3. 增加一条数据

    >>> p1 = Publisher.objects.create(name='SWht',address='haidian',city='beijing',state_province='CA',country='CN',website='http://www.songqingbo.cn')

    >>>p2 = Publisher(name='SWht2',address='haidian1',city='beijing',state_province='CA',country='CN',website='http://www.songqingbo.cn')

    >>>p2.save() #增加数据两种方式,1.使用对象的objects的create方法进行数据的创建,2.使用对象的save方法进行数据的保存。

  4. 数据库过滤查询

    >>> from article.models import Author

    >>> author_list = Author.objects.all()

    >>> author_list

    [<Author: >, <Author: >]

    >>> Author.objects.filter(first_name='shen')

    [<Author: >]

  5. 获取单个对象

    >>> Author.objects.get(first_name='shen')

    <Author: >

    >>> Author.objects.get(id='1')

    <Author: >

    >>> Author.objects.get(id='2')

    <Author: >

    注意:如果查询没有返回结果也会抛出异常

  6. 数据排序

    >>> Author.objects.orderby("firstname")

    [<Author: >, <Author: >]

    >>> Author.objects.order_by("id")

    [<Author: >, <Author: >]

  7. 数据更新

    >>> author = Author.objects.get(last_name='test')

    >>> author.last_name = 'diaoxia'

    >>> author.save()

    >>> Author.objects.all()

    [<Author: >, <Author: >]

  8. 删除对象

    >>> author = Author.objects.get(first_name='alix')

    >>> author

    <Author: >

    >>> author.delete()

    (2L, {u'article.Book_authors': 1L, u'article.Author': 1L})

    >>> Author.objects.all()

    [<Author: >]

python开发学习-day16(Django框架初识)的更多相关文章

  1. Windows上python开发--2安装django框架

    Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...

  2. Python之路,Day16 - Django 进阶

    Python之路,Day16 - Django 进阶   本节内容 自定义template tags 中间件 CRSF 权限管理 分页 Django分页 https://docs.djangoproj ...

  3. python开发学习-day15(前端部分知识、web框架、Django创建项目)

    s12-20160430-day15 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  4. Django学习(二) Django框架简单搭建

    为了快速学习Python进行Web的开发,所以我不准备从Python的基础学起,直接从Django框架入手,边学框架边学Python的基础知识. 下面就开始Django的快速开发之旅吧. 关于Djan ...

  5. python开发中常用的框架

    以下是15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Django 应该是最出名的 ...

  6. python开发学习-day01 (python安装与版本、字符串、字典、运算符、文件)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  7. Python开发工具,服务器框架等

    python服务器框架:uWSGI WEB服务器.mod_WS.Meinheld.Gunicorn.CherryPy.Bjoern 查看python内部模块命令,内置函数,查看python已经安装的模 ...

  8. 安全开发 | 如何让Django框架中的CSRF_Token的值每次请求都不一样

    前言 用过Django 进行开发的同学都知道,Django框架天然支持对CSRF攻击的防护,因为其内置了一个名为CsrfViewMiddleware的中间件,其基于Cookie方式的防护原理,相比基于 ...

  9. Python、PyCharm、Django框架安装

    一.下载Python环境 1.1 下载Python环境,以下网址: https://www.python.org/downloads/release/python-373/ 下载安装包: 1.2点击安 ...

随机推荐

  1. PHP常见数组方法和函数

    current();当前游标指向的数组单元值 next();下一个数组单元值 end()最后一个 reset()复位 prev()把数组指针往前一位 写法:$arr=array('a','b','c' ...

  2. java 数字转换成字符串

    一.各种数字类型转换成字符串型:  public static void main(String[] args) { double value = 123456.123; String str = S ...

  3. python学习笔记3-函数的递归

    递归就是指自己函数的自我调用 #递归 #自己调用自己,函数的循环 def test1(): num = int(input('please enter a number:')) if num%2==0 ...

  4. javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  5. SQL Server 2008 R2 企业版安装教程

    1 安装包解压 2 解压后,打开setup.exe文件,选择安装,显示如图: 3 选择全新安装或向现有安装添加功能 4 点确定 5 输入 企业版序列号:R88PF-GMCFT-KM2KR-4R7GB- ...

  6. 牛市必备的三个条件,A股现在还差几个

    1.国家政策 2.中美贸易 3.资金支持 A股变化如神! 自本月10日受美股大跌的影响后,A股先是随之震荡跳水,千股跌停:随后因高层力挺和政策支持而V型反转,集体涨停:接着上演过山车走势,有时涨得令人 ...

  7. debian旧日笔记

    18:45 2007-10-7 序 图形界面较好的Linux操作系统有三个主要的发行版本:RedHat, Debian, SuSE.自RedHat9后,RedHat以Fedora为名发行新的版本.仍然 ...

  8. Python Pool

    我们在使用Python时,会经常需要使用多进程/多线程的情况,以便提高程序的运行效率,尤其是跟网络进行交互,如使用爬虫时.下面我们将简单看下Python的进程池的创建,map().apply_asyn ...

  9. 逆序对 inversion

      评测传送门 [问题描述] 有一个1 − n的排列,你会依次进行m次操作,第i次操作表示为(x i , y i ),交换以这两个 值为下标的元素,每次操作有一半的概率成功,你需要求出最后序列的逆序对 ...

  10. Python练习-迭代器-模拟cat|grep文件

    代码如下: # 编辑者:闫龙 def grep(FindWhat): f=open("a.txt","r",encoding="utf8") ...