django学习4 get post 表单

来源:互联网 发布:linux下oracle启动监听 编辑:程序博客网 时间:2024/06/03 23:49

我们前面已经写好了如何在django中展示一个页面:下面我先来讲讲为什么要用MTV模式的django,那么前端人员只要写template里面的页面,留些接口给后台人员在view.py里面写函数,后台人员留些接口告诉数据库人员在model.py中写代码


在http有两种传输方式:get和post方式,比如你跳转到了template里面的Hello页面中,里面有个表单他是以get方式提交的:

<form action="/blog/search/" method="get">    <input type="text" name="key" value="" />    <input type="submit" value="submit" /></form>
这里的action有个页面,我们就要在url中实现跳转:
url(r'^blog/search/',view.function)
然后在views中添加函数
def search(request):    request.encoding = 'utf-8'    if 'key' in request.POST:        message = '你搜索的内容为: ' + request.POST['key'].encode('utf-8')    else:        message = '你提交了空表单'    return render(request,"****")

post只要吧request.get改成post就可以了
然后再form中添加
{% csrf_token %}
这样一句话。实在不行出现错误你就:在setting的页面中把一行注释了。。
在views中添加一行
from django.views.decorators.csrf import csrf_exempt

0 0
原创粉丝点击