Django学习笔记(五)Django传参

1.get:

get方式有两种:

一种是常用的get方式:http://xxx.xxx.xxx/xxx/xxx?xx=xx&xx=xx

一种是rest风格:http://xxx.xxx.xxx/xxx/xxx/xx

1.1get?方式:

urls中定义入口:

url(r'^blog/get', 'blog.views.get'),

views中定义param方法:

def get(req):
    url = req.get_full_path()
    urlParam = "http://www.baidu.com/s?wd=codeif.com&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&rsv_sug3=4&rsv_sug=1&rsv_sug1=3&rsv_sug4=74"
    o = urlparse.urlparse(urlParam)
    param_dict = urlparse.parse_qs(o.query)
    print o
    print param_dict['wd'][0]
    return render_to_response('param.html',{})

1.2Rest风格:使用到了正则表达式组的概念

urls中定义入口:组名为id,在param方法中则需要在req后面紧随id

url(r'^blog/param/(?P\d{2})$', 'blog.views.param'),
views中定义param方法:

def param(req,id):
    return render_to_response('param.html',{'id':id})

param.html页面:





{{id}}


{{id}}




2.post:

最常用的表单提交方式:

urls中定义入口:

url(r'^blog/post/$', 'blog.views.post'),
views中定义post方法,并修改post提交的入口方法:

def get(req):
    form =  UserForm()
    return render_to_response('param.html',{'form':form})

def post(req):
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            print form.cleaned_data
            return HttpResponse('OK')
    else :
        form =  UserForm()
        
        return render_to_response('param.html',{'form':form})


param.html页面:





post方式提交



提交form需要注释掉
settings文件中
MIDDLEWARE_CLASSES的验证(token验证)
{{form}}



你可能感兴趣的:(Python框架)