西维蜀黍

【Django】Django 中的时间获取与相关问题

Background

在 Django1.4以后,存在两个概念:Naive time 与 Active time:

  • Naive time就是不包含时区信息的时间,
  • Active time就是包含时区信息(time-zone-aware)的时间

举例来说,使用datetime.datetime.utcnow()或者datetime.datetime.now()输出的类似2015-05-11 09:10:33.080451,就是不带时区信息的时间(Naive time)。而使用django.util.timezone.now()输出的类似2015-05-11 09:05:19.936835+00:00的时间就是带时区信息的时间(Active time),其中+00:00表示的就是时区相对性。

  ...


【Django】Django Shell

Django shell

$ tree
.
├── core
│   └── models.py
│ 	...
└── manage.py
...
1 directory, 2 files
  ...


【Software Testing】Postman 中的 Cookies 设置

Background - 认证(Authentication)通过后记下 Cookie

使用postman模拟接口测试的时候,一般都是需要先认证(Authentication)才可以后续业务测试的,否则直接调用接口会报错“Please Login First”。每一个接口用例都去新增一个cookie值太麻烦了,因为都是一样的,所有就想着有没有一种方法可以在进行认证后(同时得到了 cookie),之后发出的每一个 HTTP 包都带上这个 Cookie。

  ...


【Nginx】macOS 下安装 Nginx

安装Nginx

更新homebrew

$ brew update

查看nginx信息

$ brew search nginx

安装nginx

$ brew install nginx

对应的配置文件地址在/etc/nginx/nginx.conf

  ...


【Node.js】使用 nvm 管理本地 Node.js 版本

Background

在我们的日常开发中经常会遇到这种情况:手上有好几个项目,每个项目的需求不同,进而不同项目必须依赖不同版的 NodeJS 运行环境。如果没有一个合适的工具,这个问题将非常棘手。

nvm 应运而生,nvm 是 Mac 下的 node 管理工具,有点类似管理 Ruby 的 rvm,如果需要管理 Windows 下的 node,官方推荐使用 nvmwnvm-windows。不过,nvm-windows 并不是 nvm 的简单移植,他们也没有任何关系。但下面介绍的所有命令,都可以在 nvm-windows 中运行。

  ...


【Django】处理 PUT 和 DELETE 方法

Context

使用django的小伙伴们应该都知道我们是无法开心的处理PUT和DELETE:

def func(request):
    if request.method == 'GET':
        s = request.GET.get('s', None)
        return XXX
    elif request.method == 'POST':
        s = request.POST.get('s', None)
        return XXX
    elif request.method == 'PUT':
        s = request.PUT.get('s', None)                    # 我们希望愉快的获取继续处理
        return XXX
    elif request.method == 'DELETE':
        s = request.DELETE.get('s', None)                # 我们希望愉快的获取继续处理
        return XXX
    else:
        pass
  ...


【Python】杂 - virtualenv 管理 Python 项目依赖

Background

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

总结来说,virtualenv 是用来隔离不同 Python 项目的开发与运行环境。一个典型的场景是,有一个基于 Python 2.7 的 Django 项目,基于的 Django 版本是 1.1,而同时我们本机上正在开发一个同样基于 Python 2.7 的 Django 项目,不同的是这个项目基于Django 1.2 版本。

我们通过 pip install django来安装 Django,而这个 Django 依赖包默认会保存在/usr/lib/python2.7/site-packages,且这两个项目均会通过访问 /usr/lib/python2.7/site-packages来完成我们的 Django 项目的编译。

这意味着,我们要么安装 1.1 的 Django 版本,要么安装 1.2的版本。

通过使用 virtualenv 后,它会在我们的工程文件夹下创建一个 venv 文件夹,里面包含了这个项目使用的 Python 可执行程序(或者称为 compiler 或者 interpreter)、所有依赖的第三方包。

最终,就解决了不同项目可能需要依赖不同版本的第三方包的问题。

  ...


【Python】Django - 连接 MySQL

MySQLdb(MySQL-python)

MySQL-python 又叫 MySQLdb,是 Python 连接 MySQL 最流行的一个驱动,很多框架都也是基于此库进行开发,遗憾的是它只支持 Python2.x,而且安装的时候有很多前置条件,因为它是基于C开发的库,在 Windows 平台安装非常不友好,经常出现失败的情况,现在基本不推荐使用,取代的是它的衍生版本。

# 前置条件
sudo apt-get install python-dev libmysqlclient-dev # Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS

# 安装
pip install MySQL-python

安装完依赖,直接使用pip安装,MySQLdb模块的名字在pip上叫MySQL-python。

$ pip install MySQL-python

mysqlclient

由于 MySQL-python 年久失修,后来出现了它的 Fork 版本 mysqlclient,完全兼容 MySQLdb,同时支持 Python3.x,是 Django ORM的依赖工具,如果你想使用原生 SQL 来操作数据库,那么推荐此驱动。安装方式和 MySQLdb 是一样的。

# Windows安装
pip install some-package.whl

# linux 前置条件
sudo apt-get install python3-dev # debian / Ubuntu
sudo yum install python3-devel # Red Hat / CentOS
brew install mysql-connector-c # macOS (Homebrew)

pip install mysqlclient

PyMySQL

PyMySQL是一个纯Python写的MySQL客户端,它的目标是替代MySQLdb,可以在CPython、PyPy、IronPython和Jython环境下运行。PyMySQL在MIT许可下发布。

步骤

1 安装 pymysql

$ pip install pymysql

2 修改 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',        #数据库名字
        'USER': 'root',          #账号
        'PASSWORD': '123456',      #密码
        'HOST': '127.0.0.1',    #IP
        'PORT': '3306',                   #端口
    }
}

3 init.py里面导入pymysql模块

# [your app in your project]/init.py

import pymysql
pymysql.install_as_MySQLdb()
  ...


【Python】杂 - pyenv 系统 Python 版本管理

Background

Understanding PATH

When you run a command like python or pip, your operating system searches through a list of directories to find an executable file with that name. This list of directories lives in an environment variable called PATH, with each directory in the list separated by a colon:

/usr/local/bin:/usr/bin:/bin

Directories in PATH are searched from left to right, so a matching executable in a directory at the beginning of the list takes precedence over another one at the end. In this example, the /usr/local/bin directory will be searched first, then /usr/bin, then /bin.

  ...


【Python】杂 - macOS 下设置 Python 默认版本

步骤

1 修改可执行文件路径

打开配置文件,指定 python 命令对应的可执行文件路径:

$ open ~/.bash_profile

增加以下:

export PATH=/usr/local/Cellar/python/3.7.4_1/bin:${PATH}

注意,如果是使用 oh-my-zsh,则需要修改添加以上内容到 ~/.zshrc中。

2 配置文件生效

$ source ~/.bash_profile
  ...