您的位置:首页 > 编程语言 > Python开发

python学习笔记(进阶)

2017-11-21 18:04 519 查看
  mysql -hlocalhost -uusername -ppassword

创建数据库(utf-8) CREATE DATABASE xiong_test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

drop命令用于删除数据库。

drop命令格式:drop database <数据库名>;

show databases;显示所有数据库

use 数据库名称; 使用数据库

select database(); 显示当前数据库的情况

创建表格

create table student(id int auto_increment primary key not null, name varchar(10) not null, gender bit defult 1, birthday datetime);

show tables;  显示所有表

desc student; 显示表格格式

alter table student add isDelete bit;添加字段

update student set name ='hha' where id =7;更新数据

delete from student where id=7;

insert into student (name) values ('刘邦');

select distinct birthday from student; 去重

select distinct id,birthday from student; 同时满足两个情况下去重

select id,name from student where id>3;

select id,name from student where id>3 and name = '赵云';

select id,name from student where id>3 or name = '赵云';

select id,name from student where not id>3 ; id不大于3的数据

like  %表示任意多个字符  _表示任意一个字符  

select * from student where name like '刘%';

select * from student where id in (1,3,5); 

select * from student where id between 1 and 3;

select * from student where id between 1 and 3 and name = '关羽';

name 关羽id = 2被打印出来

select * from student where birthday is null;

select * from student where birthday is not null;

and or 同时出现 and 优先级高  用()可改变

select count(*) from student; 获取有多少行

select max(id) from student; 求此列中的最大值

select min(id) from student; 求此列中的最小值

select sum(id) from student; 求此列的和

select avg(id) from student; 求此列的平均值 

select * from student group by gender; 分组

select * from student group by gender having id = 1;

having 对获取的结果进行筛选

where  对原始数据进行筛选  若在结果中使用会报错

asc 从小到大 升序  desc 从大到小 降序

select * from student order by id desc; 降序排列

select * from student order by id desc , name asc; 先以id降序,相同的就以name升序

select * from student where birthday is not null order by id desc;  

order by  若存在where 必须放在 where 的后面

分页规则 select * from 表名 limit start , count;

select * from student limit 2 , 2; 从索引2开始拿 拿2条数据

end yu 02-09-存储关系

on和where的选择条件的区别:

on后面的是连接条件,代表两个表建立关系所遵循的规则

where后面的可以看作是筛选条件,是对最终结果集进行过滤所遵循的规则

select * from areas as sheng inner join areas as shi on sheng.id = shi.pid;

Mete可以定义排序规则 ,下面是id倒序

class Meta:
    order=['-id']

查询的方法

filter exclude order_by values get count first返回第一个 last 

查询集中是否存在 exists

//filter的一些要义

BookInfo.books.filter(btitle__contains='传')

BookInfo.books.filter(btitle__endwith='传')

BookInfo.books.filter(btitle__isnull=False)

BookInfo.books.filter(heroinfo__hcontent__contain='八')  //英雄信息里内容包含八的英雄

F的使用(两个动态变量的对比)

BookInfo.books.filter(bread__gt=F('bcomment'))

获取 bread(阅读量大于评论量)bcomment

# coding: utf-8

name = request.POST['uname']

a1 = request.GET.getlist('a')

render(request, 'test1.html', {'a','aaa'})

调用model里面的方法 和变量一样{{ a.method}}  {{ a.bianliang }}

for 循环中  forloop.counter获取当前循环的第几次

{% for item in list %}

{% empty %}

当前没数据

{%  endfor %}

<a href="{% url'bookstest:show' %}"

第一个 ROOT URL的bookstest == namespace  

第一个 APP  URL的show == namespace  

<a href="{% url 'mobile_refund_order_list' shop.id %}?shop={{ shop.id }}">

两个shop.id是一个意思 只不过一个在url中一个正常使用  

如果需要传常量需要引号进行引用

<a href="{% url 'mobile_refund_order_list' 'aaa' %}">

模版的继承

{% extends 'base.html' %}

{% block content %}

这个区域 如果子类不写 就默认显示父类 如果写了 则父类写的不显示

{% endblock content %}结束可以不写content 但是建议写上

html转义

context=("t1","<h1>t1</h1>")

{{ t1 }} 会显示 <h1>t1</h1>

{{ t1|safe }} 会显示 h1字号的t1

{% autoescape off %}

{{ t1 }}

{% endautoescape %}

会显示 h1字号的t1,第二种关闭转义的方法,主要用于富文本编辑区

csrf

在表单form的post的请求的时候 添加csrf防止跨站请求

添加{% csrf_token %}在表单中的

原理:是在cookie 和添加了input name='csrfmiddlewaretoken' value='csrf的生成一个随机的值'

验证码 pillow包

分页的训练

list = HeroInfo.object.all()  //这个操作不会去操作数据库 因为 list没用

paginator =  Paginator(list,5) // limit = 5

page = paginator.page(1)      //取第一页的数据

content = {'page':page}

model自关联 及外键关联当前类

model.ForeignKey('self') 

AreaInfo.objects.filter(parea_isnull=True).values(); //先filter过滤出来数据  在values() 转换成字符串

django富文本编辑器 tinymce

新增数据

test1 = Test1()

test1.content = html

test1.save()

缓存 使用 django_redis_cache

haystack  全文检索的框架

whoosh 纯python 编写的全文搜索引擎性能比不上sphinx xapian

jieba中文分词的库

celery

1将耗时的操作放在celery中执行

2定时任务放在Celery当中执行

与celery名词相关

任务task:就是一个python函数

队列queque:将需要的任务放在到队列中

工人worker:在一个新进程中,负责执行队列中的任务

代理人broker:负责调度,在布置环境使用redis

查看进程中 uwsgi的进程列表 ps ajx|grep uwsgi

wsgi

WSGI相当于http 是协议行为的东西 是一个网关的接口

而uWSGI是实现WSGI的具体实现类 作用是监听所有的网关的接口

先配置

启动 uwsgi --ini uwsgi.ini

暂停 uwsgi --stop uwsgi.pid

nginx

使用nginx作用

1负载均衡 多台服务器处理很多请求时 每个服务器承受的请求大致相等

2反向代理 不直接请求服务器  请求nginx的服务器然后 转给真实的服务器

查看版本 sudo sbin/nginx -v

启动  sudo sbin/nginx

停止  sudo sbin/nginx -s stop

重启  sudo sbin/nginx -s reload

通过浏览器查看配置启动情况

一般是安装在sbin/nginx里面 所以 上面是那样子

nginx.conf学妹测试服务器中的配置

server {
        listen       80;
        server_name  www.test2.com;
        #server_name  192.168.0.121;

        access_log /data/logs/nginx/test2/access.log;
        error_log /data/logs/nginx/test2/error.log;

        root /home/django/test2;

        location / {
            proxy_pass http://127.0.0.1:9502;         }

        location /static/ {
            alias  /home/test2/static/;
            index  index.html index.htm;
        }

        location /media/ {
            alias  /home/test2/media/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~* ^(.+\.py)(.*)$ {
         expires           -1;
        }
    }

python manage.py makemigrations

python manage.py migrate
    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: