django

Django-04 GET和POST

2021-07-09  本文已影响0人  JuliusL
if reuqest.method == 'GET':
  处理GET请求时的业务逻辑
elif requset.method == 'POST':
  处理POST请求的业务逻辑
else:
  其他请求业务逻辑

1,GET处理

def test_get_post(request):
    if request.method == 'GET':
        print(request.GET)
        print(request.GET.get('c','no c'))
        print(request.GET.getlist('a'))
        pass
    elif request.method == 'POST':
        pass
    else:
        pass
    return HttpResponse('test post get')

打印:

<QueryDict: {'a': ['1', '2', '3']}>
no c
['1', '2', '3']

2,POST处理

POST_FORM = '''
<form method='post' action="/test_get_post">
  用户名:<input type="text" name="username">
  <input type='submit' value='登录'>
</form>
'''

def test_get_post(request):
    if request.method == 'GET':
        print(request.GET)
        print(request.GET.get('c','no c'))
        print(request.GET.getlist('a'))
        return HttpResponse(POST_FORM)
    elif request.method == 'POST':
        print('username is ',request.POST['username'])
        return HttpResponse('post is ok')
    else:
        pass
    return HttpResponse('test post get')
取消csrf验证
MIDDLEWARE=[
  ...
    # 'django.middleware.csrf.CsrfViewMiddleware',
  ...
]
上一篇下一篇

猜你喜欢

热点阅读