Django笔记-post与get方法相关出错记录

1、刚刚调试一个注册的程序,blog.views.register里用了return HttpResponse方法返回了一个注册页面

register.html,后者用了method = "post",但是请求的时候用到了get方法,出错如下:

Django笔记-post与get方法相关出错记录

解决方法:修改views文件,在里面同时设置post和get方法的处理函数

def register(req):
if req.method == "POST":#req.method == "GET":
uf = UserForm(req.POST)
if uf.is_valid():
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
print username,password
# User.objects.create(username=username,password=password) return HttpResponse('ok')
else:
uf = UserForm()
return render_to_response('register.html',{'uf':uf})
else :
uf = UserForm(req.GET)
if uf.is_valid():
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
print username,password
# User.objects.create(username=username,password=password) return HttpResponse('ok')
else:
uf = UserForm()
return render_to_response('register.html',{'uf':uf})

  

上一篇:day5 常用模块json和pickle


下一篇:Android文件系统编译出错记录