Django学习之-后端数据展示在前端

2022/2/21 23:26:48

本文主要是介绍Django学习之-后端数据展示在前端,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

先看前端代码

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style type="text/css">
        td,th {
            text-align: center;
            border:1px solid black;
            }
        table {
            margin-left:100px;
            margin-top:50px;
            border:1px solid black;
            border-collapse:collapse;
            }
        .align_center{
            text-align: center;
            }
    </style>
    <title>推置引擎测试界面</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
   <table width="800" >
      <caption>
        <h1>推置引擎测试界面</h1>
      </caption>
       <tr>
        <td>异常id</td>
        <td><input type="text" id="exception_id" class="text"></td>
        <td>方案id</td>
        <td><input type="text" id="plan_id" class="text"></td>
      </tr>
       <tr>
        <td colspan="4"></td>
       </tr>
       <tr>
        <td colspan="2">规则</td>
        <td>操作</td>
        <td>测试结果</td>
       </tr>
       <tr>
        <td colspan="2">testcase1:测试用例1说明</td>
        <td><input type="button" name="button1" value="测试" onclick="calculate_test1()"/></td>
        <td><span id="result1" class="align_center"></span></td>
       </tr>       
   </table>
    <script>
      function calculate_test1() {
         var exception_id = $("#exception_id").val()
         var plan_id = $("#plan_id").val()
         $.ajax({
            url: '/calculate_test1pro/',
            data: {'exception_id':exception_id,'plan_id':plan_id},
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                console.log(data)
                document.getElementById("result1").innerText = data.result
                }
            })
            }
    </script>
</body>
</html>

后端代码

from django.shortcuts import HttpResponse
from django.http import JsonResponse

# Create your views here.
def test1pro(request):
    result = "PASS"
    response = JsonResponse({"status": '服务器接收成功', 'result': result})
    return response

说明:

 



这篇关于Django学习之-后端数据展示在前端的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程