为网站配置HTTPS传输协议

2018-05-12更新

全站启用https访问,http的80端口转发到https的443端口,nginx配置可以这样写:

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
return 301 https://$host$request_uri;
} #80端口的流量转发到https

server {
listen 443;
server_name domain.com;
...
}
阅读更多

Instagram开发者权限申请

  最近想给博客做个相册,放一些日常拍摄的拿得出手的风景照,生活照。搜索一番发现,博客里放上自己的Instagram照片是个不错的选择。作为一个没有什么流量多半自娱自乐的博客,很少时间会去手动更新一个相册,相比更新Instagram,后者可是频繁而又有趣多了(虽然我也很少更新我的Ins)。想到就去做咯,下面开始我的实践记录。

阅读更多

Django 1.11 TypeError context must be a dict rather than Context

1
2
3
4
5
def current_datetime(request):
now = datetime.datetime.now()
t = get_template('current_datetime.html')
html = t.render(Context({'current_date': now}))
return HttpResponse(html)

在Django 1.11.11版本中运行会出现“TypeError context must be a dict rather than Context”的错误信息。

错误信息

查阅 Stack Overflow 得知原因:

在Django 1.8+中,模板的render方法需要传入一个参数为字典的上下文实例,即ctx({key: value})。在Django 1.10+中,render方法传递一个Context实例已被弃用。
因此上述代码在Django 1.11中应改为:

1
t.render({'current_date': now}) #get_template.render() 直接传入字典

如果仍想以传入Context实例的方式渲染模版,可以使用下面的方法:

1
2
from django.template.loader import render_to_string
t.render_to_string(Context('current_date': now))

补充:
其实,Django为渲染模板提供了更为简单的方式:

1
2
3
4
5
from django.shortcuts import render
import datetime
def current_datetime(request):
now = datetime.datetime.now() #获取当前时间
return = render(request, 'current_datetime.html', {'current_date'}: now)

virtualenvwrapper问题与解

virtualenvwrapper提供了一系列命令使得用虚拟环境工作变得愉快许多。它可以把你所有的虚拟环境都统一放在一个地方。

阅读更多

Python中的copy与deepcopy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In [1]: import copy
In [2]: a = [1, 2, 3]
In [3]: b = [4 ,5 ,6]
In [4]: c = [a, b]
In [5]: d = copy.copy(c)
In [6]: id(c)
Out[6]: 4578276616
In [7]: id(d)
Out[7]: 4578334600
In [8]: id(c[0])
Out[8]: 4578509768
In [9]: id(d[0])
Out[9]: 4578509768
In [10]: e = copy.deepcopy(c)
In [11]: id(e)
Out[11]: 4578533704
In [12]: id(e[0])
Out[12]: 4578477576

copy/deepcopy方法作用于不可变数据类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In [1]: import copy
In [2]: a = (1, 2, 3)
In [3]: b = (4, 5, 6)
In [4]: c = (a, b)
In [5]: d = copy.copy(c)
In [6]: id(c)
Out[6]: 4414753736
In [7]: id(d)
Out[7]: 4414753736
In [8]: e = copy.deepcopy(c)
In [9]: id(e)
Out[9]: 4414753736
In [10]: id(c[0])
Out[10]: 4377084768
In [11]: id(d[0])
Out[11]: 4377084768
In [12]: id(e[0])
Out[12]: 4377084768

总结:

  1. a = b,a和b指向同一内存地址。
  2. deepcopy 深拷贝,递归拷贝内部所有值,全部开辟新的内存地址。
  3. copy可变类型(比如list),最外层开辟新的内存地址,内部的值不开辟新的内存地址4. copy不可变类型(比如tuple),最外层不开辟新的内存地址。

Python多继承子类的方法溯源规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A():
def __init__(self):
pass
def save(self):
print "This is from A"
class B(A):
def __init__(self):
pass
class C(A):
def __init__(self):
pass
def save(self):
print "This is from C"
class D(B,C):
def __init__(self):
pass
test = D()
test.save()

经典类的答案: This is from A

新式类的答案: This is from C