Advanced Django (a)

choice parameters

Important in the use of some special characters or numbers on behalf of tedious string, the one to save data space, and secondly for data readability. Mainly for entering the user gender, user education, work status.

Let's take the last example:

For choicesfield, if you want to get numbers corresponding to the Chinese, not directly point field so acquired or that number, we need to use a fixed sentence 数据对象.get_字段名_display(), when there is no correspondence between the sentence acquired or digital.

MTV and MVC model

django but it is still MVC framework

MTV MVC
M:models M:models
T:templates V: views
V: views C: contronner

Ajax

Based on native js development, but to write code with native js too cumbersome, and promote the use jquery ajax to achieve

Without reloading the entire page, you can exchange data with the server and update parts of the page.

Asynchronous submitted

Synchronous asynchronous: task description is presented in a way

Synchronization: After submission of the task, return to their home to wait for the results of the task, do not do anything during the

Asynchronous: After submission of the task, no place to return to wait for the results of the task, directly next line of code, return to the task through a callback mechanism

Blocking non-blocking: the program running

Partial refresh

Not a whole page refresh, but somewhere partial page refresh, eg: github

First to an example:

Displays a front page, three page input box, to complete the digital adder in the case where the rear end toward the click of a button requesting a page refresh not

Let's look at the back end of this requirement is to write

Ajax pass json data

I need to tell the back end, my data is not urlencoded, but json format

django backend json format for the data, and will not automatically help you resolve, directly on intact to help you request.body, you can get a manual process data (transfer format, serialized)

Back-end Show:

Ajax data transfer file

  • Need to use the built-in object Formdata, the object can either pass an ordinary key-value pairs, you can also transfer files
  • There are a few parameters need to specify
    • data: Formdata (data type)
    • contentType: false (without any coding)
    • processData:false(告诉浏览器不要处理我的数据)
<body>
<input type="text" name="username" id="t1">
<input type="text" name="password" id="t2">
<input type="file" name="myfile" id="t3">
<button id="b1">提交</button>
<script>
    $('#b1').click(function () {
        // 1.先生成一个formdata对象
        var myFormData = new FormData();
        // 2.朝对象中添加普通的键值
        myFormData.append('username',$("#t1").val());
        myFormData.append('password',$("#t2").val());
        // 3.朝对象中添加文件数据
        // 1.先通过jquery查找到该标签
        // 2.将jquery对象转换成原生的js对象
        // 3.利用原生js对象的方法 直接获取文件内容
        myFormData.append('myfile',$('#t3')[0].files[0]);
        $.ajax({
            url:'',
            type:'post',
            data:myFormData,  // 直接丢对象
            // ajax传文件 一定要指定两个关键性的参数
            contentType:false,  // 不用任何编码 因为formdata对象自带编码 django能够识别该对象
            processData:false,  // 告诉浏览器不要处理我的数据 直接发就行
            success:function (data) {
                alert(data)
            }
        })
    })
</script>
</body>

contentType前后端传输数据编码

form表单

默认的提交数据的编码格式是`urlencoded`那么什么是`urlencoded`呢?

username=admin&password=123,什么等于什么的这种就是符合urlencoded数据格式

​ 问题来了!!!为什么urlencoded数据格式不需要我们自己去获取解析呢?

​ 答:django后端针对urlencoded数据格式信息会自动解析,会将结果打包给request.POST,用户只需要从request.POST即可获取对应信息

formdata

​ django后端针对formdata格式类型数据 也会自动解析,但是不会放到request.POST中而是放到了request.FILES

ajax

​ ajax默认的提交数据的编码格式也是urlencoded

django后端针对不同的编码格式数据,会有不同的处理机制以及不同的获取数据的方法

ajax + sweetalert

sweetalert是个什么鬼呢?他和bootstrap是大差不差的,我们把这个文件下载下来就行,ajax + sweetalert将会实现怎样的一个效果呢?他就是在我们去 删除一行数据的时候弹出一个下拉框,我们主要做的就是将这个下拉框中做的事情传给后端,后端再进行数据库中的数据删除

前端:

function(){
            $.ajax({
                url:'',
                type:'post',
                data:{'delete_id':$btn.attr('delete_id')},
                success:function (data) {
                    if (data.code==1000){
                        swal(data.msg, "你可以回去收拾行李跑路了.", "success");
                        // 1.直接刷新页面
                        {#window.location.reload()#}
                        // 2.通过DOM操作 实时删除
                        $btn.parent().parent().remove()
                    }else{
                        swal("发生了未知错误!", "我也不知道哪里错了.", "info");
                    }
                }
            });

序列化组件

eg:将用户表的数据 查询出来 返回给前端,给前端的是一个大字典,字典里面的数据是一个个的字段

前端是怎么写的呢?让我们瞅一眼

补充

  • django后端针对不同的编码格式数据,会有不同的处理机制以及不同的获取该数据的方法
  • 前后端在做数据交互的时候一定要标明你所发的数据到底是什么格式,数据是什么格式,就应该告诉客户什么格式
  • 序列化组件:自动帮你序列化一些数据

Guess you like

Origin www.cnblogs.com/yanjiayi098-001/p/11754639.html