<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>cao_xiao_qin's blog</title>
    <description>blog &amp; blog theme🤘 (basic template from http://xudailong.github.io)</description>
    <link>https://caoxiaoqin.github.io//</link>
    <atom:link href="https://caoxiaoqin.github.io//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 04 Dec 2018 14:10:50 +0000</pubDate>
    <lastBuildDate>Tue, 04 Dec 2018 14:10:50 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>Flask图片上传和mail邮件</title>
        <description>&lt;h3 id=&quot;flask图片上传&quot;&gt;flask图片上传&lt;/h3&gt;

&lt;h5 id=&quot;1图片需存放的路径&quot;&gt;1.图片需存放的路径&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# os.path.abspath(__file__) 拿到的是当前操作文件的绝对路径
	'/Users/apple/Desktop/python-caocaocao/6flask/day05/app/views.py'
	
	
# os.path.dirname(os.path.abspath(__file__)) 拿到的是当前操作文件的绝对路径的上一级的绝对路径
   '/Users/apple/Desktop/python-caocaocao/6flask/day05/app'


# 拿到的是当前操作文件的绝对路径的上两级的绝对路径os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
   '/Users/apple/Desktop/python-caocaocao/6flask/day05'

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;MEDIA_ROOT = os.apth.join(os.path.join(BASE_DIR,'static'), 'media')

os.path.join(BASE_DIR, 'static')拿到的路径是：
'/Users/apple/Desktop/python-caocaocao/6flask/day05/static'

MEDIA_ROOT = '/Users/apple/Desktop/python-caocaocao/6flask/day05/static/media'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;2获取图片&quot;&gt;2.获取图片&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;upload_img = request.files.get('img')
# 获取文件或图片： files
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;3保存图片到本地指定的路径&quot;&gt;3.保存图片到本地（指定的路径）&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path = os.path.join(MEDIA_ROOT, upload_img.filename)
upload_img.save(path)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;4保存图片到数据库&quot;&gt;4.保存图片到数据库&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;models.py： 
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

# 创建存放图片的数据库表
class UploadImg(db.Model):
    id = db.Column(db.Integer, 
                   primary_key=True,autoincrement=True)
    img = db.Column(db.String(100), nullable=False)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;up_img = UploadImg()
upload_path = upload_img.filename
up_img.img = upload_path
db.session.add(up_img)
db.session.commit()
return render_template('img.html', img=up_img)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;flask-mail&quot;&gt;Flask-mail&lt;/h3&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;pip install flask-mail&lt;/code&gt;

&lt;h5 id=&quot;1邮箱配置&quot;&gt;1.邮箱配置&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Config():
    # 邮箱配置
    MAIL_SERVER = 'smtp.126.com'
    MAIL_PORT = 465
    MAIL_USE_TLS = False
    MAIL_USE_SSL = True
    MAIL_USERNAME = 'cao_xiao_qin@126.com'
    MAIL_PASSWORD = 'cao13518479723'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;2发送消息&quot;&gt;2.发送消息&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@blue.route('/mail_hello/')
def mail_hello():
	msg = Message('title',
				  sender=Config.MAIL_USERNAME,
				  recipients=['915710285@qq.com'])
	msg.body = '比你优秀的人 还比你努力'
	mail.send(msg)
	return 'success'

# Config.MAIL_USERNAME = 'cao_xiao_qin@126.com'
# title： 标题
# sender: 发送者
# recipients ：接受者
# body： 发送的内容
# send(): 发送
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Thu, 29 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/11/29/flask%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0%E5%92%8Cmail%E9%82%AE%E4%BB%B6/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/11/29/flask%E5%9B%BE%E7%89%87%E4%B8%8A%E4%BC%A0%E5%92%8Cmail%E9%82%AE%E4%BB%B6/</guid>
        
        
      </item>
    
      <item>
        <title>Djangoday02</title>
        <description>&lt;h2 id=&quot;一创建项目day02&quot;&gt;一、创建项目day02&lt;/h2&gt;
1.进入虚拟环境：&lt;code class=&quot;highlighter-rouge&quot;&gt;source activate &lt;/code&gt;&lt;br /&gt;
2.创建代码的项目：&lt;code class=&quot;highlighter-rouge&quot;&gt;pyhton manage.py startproject day02&lt;/code&gt; &lt;br /&gt;
3.更改设置__init__.py设置
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    import pymysql  
	 pymysql.install_as_MySQLdb()   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
4.更改setting.py设置

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'dj8',
    'USER': 'root',
    'HOST': '127.0.0.1',
    'PORT': 3306,
    'PASSWORD': '123456'
}
}   
LANGUAGE_CODE = 'zh-hans'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
 二、创建应用    
` python manage.py startapp &amp;lt;文件名&amp;gt;`
#### 我先创建了一个名为app的应用：   
`python manage.py startapp app`    
然后在app里面的models.py里定义东西  

   
from django.db import models  
class Student(models.Model):  
     定义s_name字段最长不超过6个字符    
     unique=True 唯一      
    s_name = models.CharField(max_length=6, unique=True)
          
     定义s_age字段    
    s_age = models.IntegerField(default=18)
      
     定义s_gender字段  
    s_gender = models.BooleanField(default=1) 
      
      定义create_time字段 创建时间   
    create_time = models.DateTimeField(auto_now_add=True)
      
    #### 定义operate_time字段 修改时间 auto_now为每次修改 保存每次修改的时间   
    operate_time = models.DateTimeField(auto_now=True)    
	
	class Meta:  
        #### 定义模型迁移导数据库的表名  
        #### 表名为student  
        db_table = 'student'  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Thu, 29 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/11/29/djangoday02/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/11/29/djangoday02/</guid>
        
        
      </item>
    
      <item>
        <title>Django</title>
        <description>&lt;h3 id=&quot;mvcmode-view-controller&quot;&gt;MVC(Mode View Controller)&lt;/h3&gt;
Model: 即数据存取层。用于封装于应用程序的业务逻辑相关的数据，以及对数据的处理。说白了就是模型对象负责在数据库中存取数据

View: 即表现层。负责数据的显示和呈现。渲染的html页面给用户，或者返回数据给用户。

Controller: 即业务逻辑层。负责从用户端收集用户的输入，进行业务逻辑处理，包括向模型中发送数据，进行CRUD操作。

&lt;h3 id=&quot;django的模式简介---mvt模式&quot;&gt;Django的模式简介 - MVT模式&lt;/h3&gt;
严格来说，Django的模式应该是MVT模式，本质上和MVC没什么区别，也是各组件之间为了保持松耦合关系，只是定义上有些许不同。

Model： 负责业务与数据库(ORM)的对象

View： 负责业务逻辑并适当调用Model和Template

Template: 负责把页面渲染展示给用户

注意： Django中还有一个url分发器，也叫作路由。主要用于将url请求发送给不同的View处理，View在进行相关的业务逻辑处理。

&lt;h3 id=&quot;安装虚拟环境&quot;&gt;安装虚拟环境&lt;/h3&gt;
&lt;h5 id=&quot;1安装virtualenv&quot;&gt;1、安装VIRTUALENV&lt;/h5&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;pip install virtualenv&lt;/code&gt;

&lt;h3 id=&quot;创建虚拟环境&quot;&gt;创建虚拟环境&lt;/h3&gt;
&lt;h5 id=&quot;2进入你需要创建虚拟环境的目录&quot;&gt;2、进入你需要创建虚拟环境的目录&lt;/h5&gt;
```virtualenv –no-site-packages djenv   &lt;br /&gt;
参数说明： –no-site-packages 是获取一个纯净的python虚拟环境&lt;br /&gt;
         -p + &lt;版本路径&gt;指定python版本   
         djenv 是文件名 可以随意（但需见名知意）&lt;/版本路径&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;###   3、进入和退出env   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
进入: cd djenv  再使用activate命令
注意：mac 需要 source activate命令才能进入&lt;br /&gt;
退出： deactivate
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;### 二、安装Django  
安装django
`pip install django==1.11`
 查看：
pip list      
pip freeze   

    
安装数据库的驱动 
  
`pip install pymysql`

### 三、创建Django项目
`django-admin startproject&amp;lt;文件名&amp;gt;`
例如：django-admin startproject day01   

用pycharm打开项目，并使用创建好的虚拟环境

Django目录结构：

  .   
  ├── day01   
  │   ├── __init__.py  
  │   ├── settings.py  
  │   ├── urls.py   
  │   └── wsgi.py    
  └── manage.py 
终端启动:

`python manage.py runserver IP:PORT`
 django启动项目:python manage.py runserver 8080
 8080 是指定端口  

 
修改manage.py 中：

`LANGUAGE_CODE = 'zh-hans' `- 字体

`ALLOWED_HOSTS = ['*']`

启动添加0.0.0.0表示允许所有ip访问


### 四、连接数据库
修改manager.py文件

seettings.py数据库配置
DATABASES = {  
   'default': {  
      'ENGINE': 'django.db.backends.mysql',  
      'NAME': 'dj6',  
      'USER': 'root',    
      'PASSWORD': '123456',  
      'HOST': '127.0.0.1',  
      'PORT': 3306  
  }  
} 

### 安装数据库的驱动
pip install pymysql

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
修改__init__.py文件
在__init__ 设置里面

因为python3不能直接连接数据库

import pymysql

pymysql.install_as_MySQLdb()
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;### 生成一些的对应表（映射模型的数据库）

`python manage.py migrate`

### 创建管理用户
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
python manage.py  createsuperuser
createsuperuser例如：admin + 密码
```

访问管理后台（admin）网络查看&lt;br /&gt;
` http://127.0.0.1:8000/admin/login/?next=/admin/`

</description>
        <pubDate>Thu, 29 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/11/29/django/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/11/29/django/</guid>
        
        
      </item>
    
      <item>
        <title>Django 上线</title>
        <description>使用centos部署django （上线）

安装命令

&lt;h3 id=&quot;安装mariadb&quot;&gt;安装MariaDB&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum -y install mariadb mariadb-server

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

安装完成MariaDB，首先启动MariaDB

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl start mariadb

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

设置开机启动

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl enable mariadb

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;设置密码&quot;&gt;设置密码&lt;/h3&gt;

命令: mysql_secure_installation

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Enter current password for root:&amp;lt;–初次运行直接回车

设置密码

Set root password? [Y/n] &amp;lt;– 是否设置root用户密码，输入y并回车或直接回车

New password: &amp;lt;– 设置root用户的密码
Re-enter new password: &amp;lt;– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n] &amp;lt;– 是否删除匿名用户，回车

Disallow root login remotely? [Y/n] &amp;lt;–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] &amp;lt;– 是否删除test数据库，回车

Reload privilege tables now? [Y/n] &amp;lt;– 是否重新加载权限表，回车

初始化MariaDB完成，接下来测试登录

mysql -u root -p

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;开启远程连接&quot;&gt;开启远程连接&lt;/h3&gt;

在mysql数据库中的user表中可以看到默认是只能本地连接的，所有可以添加一个新的用户，该用户可以远程访问

&lt;h4 id=&quot;1-创建用户&quot;&gt;1. 创建用户&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 先使用数据库
use mysql;

# 针对ip
create user 'root'@'192.168.10.10' identified by 'password';

#全部
 create user 'root'@'%' identified by 'password';

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;2-授权&quot;&gt;2. 授权&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 给用户最大权限
grant all privileges on *.* to 'root'@'%' identified by 'password';

# 给部分权限(test 数据库)

grant all privileges on test.* to 'root'@'%' identified by 'password' with grant option;

# 刷新权限表
flush privileges;

# 查看
show grants for 'root'@'localhost';

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

接下来就可以在远程的数据库可视化工具中直接访问该服务器中的mysql了。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 访问数据库
mysql -u root -p

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;安装python36&quot;&gt;安装python3.6&lt;/h3&gt;

在centos中，系统默认只提供python2.7的版本，但是项目我们使用的python3.6的版本。所有我们自己安装python3

&lt;h4 id=&quot;安装python3的方法&quot;&gt;安装Python3的方法&lt;/h4&gt;

首先安装依赖包

安装Python3.6所需要的依赖包：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum -y groupinstall &quot;Development tools&quot;

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

安装Python3.7还需额外安装依赖包：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum install libffi-devel -y

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

然后根据自己需求下载不同版本的Python3，我下载的是Python3.6.2

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

然后解压压缩包，进入该目录，安装Python3

tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make &amp;amp;&amp;amp; make install

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

最后创建软链接

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ln -s /usr/local/python3/bin/python3 /usr/bin/python3

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;安装环境&quot;&gt;安装环境&lt;/h3&gt;

&lt;h4 id=&quot;1-安装virtualenv&quot;&gt;1. 安装virtualenv&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum install python-virtualenv

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;2-创建虚拟环境&quot;&gt;2. 创建虚拟环境&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;virtualenv --no-site-packages env

cd env

# 激活虚拟环境
source &amp;lt;目录&amp;gt;bin/activate

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;3-安装环境需要的包&quot;&gt;3. 安装环境需要的包&lt;/h4&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/home/env/freshenv/bin/pip3 install -r reqyirement.txt

reqyirement.txt文件中记录的是需要安装包的名称以及对应的版本

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;部署&quot;&gt;部署&lt;/h3&gt;

</description>
        <pubDate>Thu, 29 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/11/29/django-%E4%B8%8A%E7%BA%BF/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/11/29/django-%E4%B8%8A%E7%BA%BF/</guid>
        
        
      </item>
    
      <item>
        <title>Diango Rest分页查看和过滤</title>
        <description>&lt;h3 id=&quot;diango-rest分页查看和过滤&quot;&gt;diango-rest分页查看和过滤&lt;/h3&gt;

settings.py设置配置文件

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# rest_framework配置
REST_FRAMEWORK = {
    # 设置分页
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 2,
    # 过滤配置
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework.filters.DjangoFilterBackend',
        'rest_framework.filters.SearchFilter'
    )
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在视图文件里：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;from django.shortcuts import render
from rest_framework import mixins, viewsets

from apps.filters import ArticleFilter
from apps.models import Article
from apps.serializers import ArticleSerializer


class ArticleView(viewsets.GenericViewSet,
                  mixins.ListModelMixin,
                  mixins.DestroyModelMixin,
                  mixins.UpdateModelMixin,
                  mixins.RetrieveModelMixin,
                  mixins.CreateModelMixin):
    # 查询数据
    queryset = Article.objects.filter(is_delete=0)
    # 序列化
    serializer_class = ArticleSerializer
    # 过滤
    filter_class = ArticleFilter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在apps/filters.py

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;django_filters&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest_framework&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;apps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;models&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Article&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArticleFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FilterSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;127.0.0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;查询内容&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;内容&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;django_filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CharFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lookup_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'icontains'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;django_filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CharFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'desc'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lookup_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'icontains'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;create_time_min&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;django_filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTimeFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'create'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lookup_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'gt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;create_time_max&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;django_filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTimeFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'create'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lookup_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'lt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Article&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

通过postman# 127.0.0.1:8080/apps/article/?t=查询内容&amp;amp;desc=内容&amp;amp;create_time_min=时间

</description>
        <pubDate>Thu, 29 Nov 2018 00:00:00 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/11/29/diango-rest%E5%88%86%E9%A1%B5%E6%9F%A5%E7%9C%8B%E5%92%8C%E8%BF%87%E6%BB%A4/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/11/29/diango-rest%E5%88%86%E9%A1%B5%E6%9F%A5%E7%9C%8B%E5%92%8C%E8%BF%87%E6%BB%A4/</guid>
        
        
      </item>
    
      <item>
        <title>页面跳转</title>
        <description>day04
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;实现重定向：HttpResponseRedirect()  
实现渲染：render()
实现响应： HttpResponse()  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
硬编码例如
&lt;code class=&quot;highlighter-rouge&quot;&gt;'/add/index/'， '/add/all_stu/ &lt;/code&gt;   &lt;br /&gt;
第一种重定向    &lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;return HttpResponseRedirect('/add/index/')&lt;/code&gt; &lt;br /&gt;
第二种重定向 反向解析reverse(‘namespace:name’) 也就是先定义别名  &lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;return HttpResponseRedirect(reverse('dy:all'))&lt;/code&gt;

</description>
        <pubDate>Fri, 26 Oct 2018 19:21:39 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/10/26/%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/10/26/%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC/</guid>
        
        <category>Django</category>
        
        
        <category>Django</category>
        
      </item>
    
      <item>
        <title>页面跳转</title>
        <description>day04
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;实现重定向：HttpResponseRedirect()  
实现渲染：render()
实现响应： HttpResponse()  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
硬编码例如
&lt;code class=&quot;highlighter-rouge&quot;&gt;'/add/index/'， '/add/all_stu/ &lt;/code&gt;   &lt;br /&gt;
第一种重定向    &lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;return HttpResponseRedirect('/add/index/')&lt;/code&gt; &lt;br /&gt;
第二种重定向 反向解析reverse(‘namespace:name’) 也就是先定义别名  &lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;return HttpResponseRedirect(reverse('dy:all'))&lt;/code&gt;

</description>
        <pubDate>Fri, 26 Oct 2018 19:21:39 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/10/26/%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/10/26/%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC/</guid>
        
        <category>Django</category>
        
        
        <category>Django</category>
        
      </item>
    
      <item>
        <title>django_pages01</title>
        <description>alt+enter：快捷键

autofield charfield integerfield booleanfield datetimefield  &lt;br /&gt;
filter()查询满足条件的 &lt;br /&gt;
first() &lt;br /&gt;
last()最后 &lt;br /&gt;
edxclude() 排除满足条件的 &lt;br /&gt;
all()查询所有&lt;br /&gt;
order_by()排序 &lt;br /&gt;
values()   &lt;br /&gt;
__contains() 包含 &lt;br /&gt;
__startwith() 以什么开头&lt;br /&gt;
__endwith() 以什么结尾 &lt;br /&gt;
__gt() 大于&lt;br /&gt;
__gte() 大于等于

&lt;h2 id=&quot;一删除&quot;&gt;一、删除&lt;/h2&gt;
在urls.py添加 &lt;br /&gt;
url(r’del_stu/’, views.del_stu), &lt;br /&gt;
在views里添加一个函数

def del_stu(request): &lt;br /&gt;
Student.objects.filter(s_name=’caocao’).delete()&lt;br /&gt;
return HttpResponse(‘删除成功’)

&lt;h2 id=&quot;二-更新&quot;&gt;二、 更新&lt;/h2&gt;
在urls.py添加 &lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;url(r'update_stu/', views.update_stu),&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
在views里添加一个函数

def update_stu(request):&lt;br /&gt;
Student.objects.filter(s_name=’被更新的据’).update(s_name=’更新数据’)  &lt;br /&gt;
return HttpResponse(‘更新成功’)

&lt;h5 id=&quot;from-djangodbmodels-import-q-f-需要导入&quot;&gt;from django.db.models import Q, F 需要导入&lt;/h5&gt;
&lt;h5 id=&quot;或者条件&quot;&gt;或者条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(Q(s_age=18) | Q(s_gender=1))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;且条件&quot;&gt;且条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(Q(s_age=18) &amp;amp; Q(s_gender=1))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;非条件&quot;&gt;非条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(~Q(s_age=18))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;chinese成绩大于math成绩&quot;&gt;chinese成绩大于math成绩&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(chinese__gt=F('math'))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;

</description>
        <pubDate>Thu, 25 Oct 2018 20:09:04 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/10/25/django_pages01/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/10/25/django_pages01/</guid>
        
        <category>Django</category>
        
        
        <category>django</category>
        
      </item>
    
      <item>
        <title>django_pages01</title>
        <description>alt+enter：快捷键

autofield charfield integerfield booleanfield datetimefield  &lt;br /&gt;
filter()查询满足条件的 &lt;br /&gt;
first() &lt;br /&gt;
last()最后 &lt;br /&gt;
edxclude() 排除满足条件的 &lt;br /&gt;
all()查询所有&lt;br /&gt;
order_by()排序 &lt;br /&gt;
values()   &lt;br /&gt;
__contains() 包含 &lt;br /&gt;
__startwith() 以什么开头&lt;br /&gt;
__endwith() 以什么结尾 &lt;br /&gt;
__gt() 大于&lt;br /&gt;
__gte() 大于等于

&lt;h2 id=&quot;一删除&quot;&gt;一、删除&lt;/h2&gt;
在urls.py添加 &lt;br /&gt;
url(r’del_stu/’, views.del_stu), &lt;br /&gt;
在views里添加一个函数

def del_stu(request): &lt;br /&gt;
Student.objects.filter(s_name=’caocao’).delete()&lt;br /&gt;
return HttpResponse(‘删除成功’)

&lt;h2 id=&quot;二-更新&quot;&gt;二、 更新&lt;/h2&gt;
在urls.py添加 &lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;url(r'update_stu/', views.update_stu),&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
在views里添加一个函数

def update_stu(request):&lt;br /&gt;
Student.objects.filter(s_name=’被更新的据’).update(s_name=’更新数据’)  &lt;br /&gt;
return HttpResponse(‘更新成功’)

&lt;h5 id=&quot;from-djangodbmodels-import-q-f-需要导入&quot;&gt;from django.db.models import Q, F 需要导入&lt;/h5&gt;
&lt;h5 id=&quot;或者条件&quot;&gt;或者条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(Q(s_age=18) | Q(s_gender=1))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;且条件&quot;&gt;且条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(Q(s_age=18) &amp;amp; Q(s_gender=1))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;非条件&quot;&gt;非条件&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(~Q(s_age=18))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;h5 id=&quot;chinese成绩大于math成绩&quot;&gt;chinese成绩大于math成绩&lt;/h5&gt;
&lt;strong&gt;&lt;em&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;stus = Student.objects.filter(chinese__gt=F('math'))&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;

</description>
        <pubDate>Thu, 25 Oct 2018 20:09:04 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/10/25/django_pages01/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/10/25/django_pages01/</guid>
        
        <category>Django</category>
        
        
        <category>django</category>
        
      </item>
    
      <item>
        <title>Django_models</title>
        <description>&lt;h2 id=&quot;一创建项目day02&quot;&gt;一、创建项目day02&lt;/h2&gt;
1.进入虚拟环境：&lt;code class=&quot;highlighter-rouge&quot;&gt;source activate &lt;/code&gt;&lt;br /&gt;
2.创建代码的项目：&lt;code class=&quot;highlighter-rouge&quot;&gt;pyhton manage.py startproject day02&lt;/code&gt; &lt;br /&gt;
3.更改设置__init__.py设置
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import pymysql  
pymysql.install_as_MySQLdb()   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
4.更改setting.py设置

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘dj8’,
‘USER’: ‘root’,
‘HOST’: ‘127.0.0.1’,
‘PORT’: 3306,
‘PASSWORD’: ‘123456’
}
} &lt;br /&gt;
LANGUAGE_CODE = ‘zh-hans’

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
二、创建应用    
` python manage.py startapp &amp;lt;文件名&amp;gt;`
#### 我先创建了一个名为app的应用：   
`python manage.py startapp app`    
然后在app里面的models.py里定义东西  


from django.db import models  
class Student(models.Model):  
定义s_name字段最长不超过6个字符    
unique=True 唯一      
s_name = models.CharField(max_length=6, unique=True)

定义s_age字段    
s_age = models.IntegerField(default=18)

定义s_gender字段  
s_gender = models.BooleanField(default=1) 

定义create_time字段 创建时间   
create_time = models.DateTimeField(auto_now_add=True)

#### 定义operate_time字段 修改时间 auto_now为每次修改 保存每次修改的时间   
operate_time = models.DateTimeField(auto_now=True)    

class Meta:  
#### 定义模型迁移导数据库的表名  
#### 表名为student  
db_table = 'student'  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Tue, 23 Oct 2018 19:07:02 +0000</pubDate>
        <link>https://caoxiaoqin.github.io//2018/10/23/Django_models/</link>
        <guid isPermaLink="true">https://caoxiaoqin.github.io//2018/10/23/Django_models/</guid>
        
        <category>Django</category>
        
        
        <category>Django</category>
        
      </item>
    
  </channel>
</rss>
