get请求


练习:
views.py
def test_method(request):
if request.method == 'get':
print(request.get)
# 如果链接中没有参数a会报错
print(request.get['a'])
# 使用这个方法,当查询不到参数时,不会报错而是返回你设置的值
print(request.get.get('c','no c'))
# 当链接中传入多个a时,会返回列表;如果使用上面的两个方法时,只会返回最后一个值
print(request.get.getlist('a'))
elif request.method == 'post':
pass
return httpresponse('ok')
urls.py
path('test_method', views.test_method)
地址:
http://localhost:8000/test_method?a=1
响应:
post请求:
练习:
views.py
form = """
<form action="/test_method" method="post">
用户名: <input type="text" name="name">
<input type="submit" value="提交">
</form>
"""
def test_method(request):
if request.method == 'get':
print(request.get)
# 如果链接中没有参数a会报错
print(request.get['a'])
# 使用这个方法,当查询不到参数时,不会报错而是返回你设置的值
print(request.get.get('c', 'no c'))
# 当链接中传入多个a时,会返回列表;如果使用上面的两个方法时,只会返回最后一个值
print(request.get.getlist('a'))
return httpresponse(form)
elif request.method == 'post':
print(request.post['name'])
return httpresponse('post ok')
return httpresponse('ok')
urls.py
path('test_method', views.test_method)
链接: http://localhost:8000/test_method?a=1
当我门直接访问时会出触发django的csrf检测
关闭csrf检测的方法
post处理:
发表评论