多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC=1,5] >[success] # ajax 写法 >[danger] ##### views ~~~ from django.shortcuts import render,redirect,HttpResponse import json from . import models from . import account # Create your views here. def login(request): if request.method == "GET": obj = account.loginFrom() return render(request,'login.html',{'obj':obj}) if request.method == "POST": ret = {'status':True,'message':None } obj = account.loginFrom(request.POST) valid = obj.is_valid() if valid: models.userinfo.objects.filter(**obj.cleaned_data) return HttpResponse(json.dumps(ret)) ret['status'] = False ret['message'] = obj.errors return HttpResponse(json.dumps(ret)) ~~~ >[danger] ##### html 这个ajax 判断不能直接用只是一种思路 ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form id="fm"> {% csrf_token %} <p>{{ obj.user }}<span class="us_error"></span></p> <p>{{ obj.pwd }}<span class="pw_error"></span></p> <input id="btn" type="button" value="提交按钮"> </form> <script src="/static/jquery-1.12.4.js"></script> <script src="/static/jquery.cookie.js"></script> <script> $(function () { BindSumbit(); }); function BindSumbit() { $('#btn').click( function () { $.ajax({ url:'/login.html', type:"POST", data:$("#fm").serialize(), dataType:'JSON', success:function (arg) { if(arg.status){ window.location.href="http://www.baidu.com" }else{ $('.us_error').text(arg.message.user[0]); $('.pw_error').text(arg.message.pwd[0]) } } }) } ) } </script> </body> </html> ~~~