post请求和数据提交
创建一个简单表单
(env) [root@ES-10-1-21-55-B28 dashboard]# cat templates/test.html <form method='post'> <ul> {% csrf_token %} <li>用户名: <input type="text" name="username"></li> <li>密码: <input type="password" name="userpass"></li> <li><input type="submit"></li> </ul> </form>
方法调用
(env) [root@ES-10-1-21-55-B28 dashboard]# cat views.py from django.shortcuts import render # Create your views here. from django.http import HttpResponse,JsonResponse def index(request): print (request.POST) if request.method=="POST": username=request.POST.get('username',"") userpass=request.POST.get('userpass',"") print(username,userpass) if username == "ljq" and userpass == "123": return HttpResponse('登录成功') else: return HttpResponse('登录失败') else: return render(request,'test.html')