Django Form Component--ajax Commit

Source: Internet
Author: User

If you want Ajax to submit data to the Django backend, to jump to the page, you need to jump in Ajax, in the background to jump does not take effect

Sample code

Views section

Rom django.shortcutsImportRender, redirect, HttpResponse#Create your views here. fromDay60app.form_clsImportaformImportJSON fromDjango.forms.utilsImporterrordictdefAjax (Request):ifRequest.method = ="GET": obj=aform ()returnRender (Request,"ajax.html", {"obj": obj}) elifRequest.method = ="POST": obj=aform (Request. POST) ret= {"Status": None,"message": None}Print("----", obj)ifobj.is_valid (): ret["Status"] ="Success"            Print(" *", ret)returnHttpResponse (Json.dumps (ret))Else:            #err = obj.errorsret["message"] =obj.errorsreturnHttpResponse (Json.dumps (ret))

From section

 fromDjango.formsImportForm fromDjango.formsImportWidgets fromDjango.formsImport Fields fromDjango.forms.modelsImportModelchoicefield fromDay60appImportModelsclassaform (Form): user_id=Fields . Integerfield (widget=widgets. Select (choices=[(1,"Raiei"), (2,"Chunyun"), (3,"Star") ] ) age= fields. Integerfield ()

Front Page Section

<! DOCTYPE html>"en">"UTF-8"> <title>Title</title>"FM"action="/ajax/"Method="Post"Novalidate>{{obj.as_p}}<input type="Button"Value="Ajax Submissions"Id=" but"> </form> <script src="/static/jquery-3.1.1.js"></script> <script>$ (function () {$ ("#but"). Click (function () {$.ajax ({URL:"/ajax/", type:"POST", data:$ ("#fm"). Serialize (), DataType:"JSON", Success:function (ARG) {Console.log ("----", ARG); if(arg["Status"] =="Success") {window.location.href="http://www.jd.com"                        }{#else:#}                    }                })            })        })    </script></body>

Django Form Component--ajax Commit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.