|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
django, CSRF token missing
or
incorrect
CSRF token missing
or
incorrect
-
-
1
在 templete 中, 为每个 POST form 增加一个 {
%
csrf_token
%
} tag. 如下:
<form>
{
%
csrf_token
%
}
<
/
form>
2
在 view 中, 使用 django.template.RequestContext 而不是 Context.
render_to_response, 默认使用 Context. 需要改成 RequestContext.
导入
class
:
from
django.template
import
RequestContext
给 render_to_response 增加一个参数:
def
your_view(request):
...
return
render_to_response(
'template.html'
,
your_data,
context_instance
=
RequestContext(request)
)
|
MySQLdb的使用
|
1
2
3
4
5
|
import MySQLdb
db = MySQLdb.
connect
(
user
=
'oadmin'
, db=
'admin'
, passwd=
'admin'
, host=
'localhost'
)
cursor
=db.
cursor
()
cursor
.
execute
(
'select * from admin'
)
db.
close
()
|
将字符串转换为字典
|
1
2
3
|
d1
=
"{1:'a'}"
import
ast
ast.literal_eval(d1)
|
判断是否为json数据
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import
json
def
is_json(myjson):
try
:
json_object
=
json.loads(myjson)
except
ValueError, e:
return
False
return
True
print
is_json(
"{}"
)
#prints True
print
is_json(
"{asdf}"
)
#prints False
print
is_json(
'{ "age":100}'
)
#prints True
print
is_json(
"{'age':100 }"
)
#prints False
print
is_json(
"{\"age\":100 }"
)
#prints True
|
本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/1259785,如需转载请自行联系原作者