西维蜀黍

【Python】Basics - 函数参数

定义函数

>>> def fib(n):    # write Fibonacci series up to n
...     """Print a Fibonacci series up to n."""
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
  ...


【Django】Django Form

Django’s role in forms

Django’s form functionality can simplify and automate vast portions of this work, and can also do it more securely than most programmers would be able to do in code they wrote themselves.

Django handles three distinct parts of the work involved in forms:

  • preparing and restructuring data to make it ready for rendering
  • creating HTML forms for the data
  • receiving and processing submitted forms and data from the client

It is possible to write code that does all of this manually, but Django can take care of it all for you.

  ...


【SQL】清空表数据后如何让自增 ID 从 1 开始

Problem

MySQL清空表中数据后,如何让自增ID从1开始?

Solution

清空表中数据时,使用truncate命令,而不用delete命令。

mysql> truncate test;
  ...


【MySQL】ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key

Problem

mysql删除主键重复ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key

Analysis

若一个table 中的某个column是其他table的foreign key,那么,就不能使用对该table 使用truncate 或者drop SQL命令。

  ...


【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,官方推荐使用 nvmw 或 nvm-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
  ...