学习Django,添加一个add.py,出现403 forbidden错误
解决方法:
在mysite文件夹下,修改settings.py,添加一行 'django.middleware.csrf.CsrfResponseMiddleware', 即可解决问题.
...
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
...
add.py
from django.http import HttpResponse text = """<form method="post" action="/add/"> <input type="text" name="a" value="%d"> + <input type="text" name="b" value="%d"> <input type="submit" value="="> <input type="text" value="%d"> </form>""" def index(request): if request.POST.has_key('a'): a = int(request.POST['a']) b = int(request.POST['b']) else: a = 0 b = 0 return HttpResponse(text % (a, b, a + b))