首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在发送新GET请求之前删除请求记录

在发送新GET请求之前删除请求记录
EN

Stack Overflow用户
提问于 2021-12-15 13:58:33
回答 1查看 44关注 0票数 2

我正在使用Django构建一个web应用程序。我不太熟悉javascript/html,因为它不是我的专长领域。

我所做的是搜索一个名称,该名称将在api中查找并与其他信息一起返回。

我会贴出我认为是针对我的问题的代码。如果你还需要什么,我可以提供。

views.py

代码语言:javascript
运行
复制
from django.shortcuts import  render
from .models import customer
import requests

def get_customer(request):
    all_customers = {}
    if 'name' in request.GET:
        name = request.GET['name']
        url = 'https://retoolapi.dev/aSIZJV/customer__data?FirstName=%s'%name

        response = requests.get(url)
        data = response.json()
        customers = data

        for i in customers:
            customer_data = customer(
                uid = i['id'],
                f_name = i['FirstName'],
                m_name = i['MiddleName'],
                l_name = i['LastName'],
                phone = i['Phone'],
                email = i['Email'],
                DOB=i['DateOfBirth'],
                EID=i['EmiratesID']

            )
            customer_data.save()
            all_customers = customer.objects.all().order_by('-id')

    return render (request, 'customer.html', { "all_customers": all_customers} )

customer.html

代码语言:javascript
运行
复制
{% extends 'base.html'%}
{% load static %}
{% block content %}

  <div class = "container">
    <div class = "text-center container">
      <br>
      <h2 class = "text-center">Search for the desired customer</h2>
      <br>
      <form method="GET">
        <input type='button' value='Remove Table Body' onclick='removeTableBody()'/>
          
<!--          I am trying to remove the table body using the line above, but it is not working-->
          
          
        <input type = "text" name = "name" placeholder="Search..." class = "text-center"> 
        <button type = "submit" class = "btn-danger btn-sm">SEARCH CUSTOMER</button>
      </form>

    </div>
    <br><br>


    <div class="container">
    <h1>Customer Table</h1>

    <div id="toolbar">
            <select class="form-control">
                    <option value="">Export Basic</option>
                    <option value="all">Export All</option>
                    <option value="selected">Export Selected</option>
            </select>
    </div>

    <table id="table"
                 data-toggle="table"
                 data-search="true"
                 data-filter-control="true"
                 data-show-export="true"
                 data-click-to-select="true"
                 data-toolbar="#toolbar">
        <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-field="ID">ID</th>
                <th data-field="FirstName" data-filter-control="input" data-sortable="true">First Name</th>
                <th data-field="MiddleName" data-filter-control="input" data-sortable="true">Middle Name</th>
                <th data-field="LastName" data-filter-control="input" data-sortable="true">Last Name</th>
                <th data-field="Phone" data-filter-control="select" data-sortable="true">Phone</th>
                <th data-field="Email" data-filter-control="select" data-sortable="true">Email</th>
                <th data-field="DateOfBirth" data-filter-control="select" data-sortable="true">Date Of Birth</th>
                <th data-field="EmiratesID" data-filter-control="select" data-sortable="true">EmiratesID</th>
            </tr>
        </thead>
        <tbody>
            {% for customer in all_customers %}
                <tr>
                    <td class="bs-checkbox "><input data-index="0" name="btSelectItem" type="checkbox"></td>
                    <td>{{customer.uid}}</td>
                    <td>{{customer.f_name}}</td>
                    <td>{{customer.m_name}}</td>
                    <td>{{customer.l_name}}</td>
                    <td>{{customer.phone}}</td>
                    <td>{{customer.email}}</td>
                    <td>{{customer.DOB}}</td>
                    <td>{{customer.EID}}</td>
                </tr>
            {% endfor %}

        </tbody>
    </table>
    </div>
  </div>
{% endblock %}
<!-- partial -->

static/website/dist/script.js

代码语言:javascript
运行
复制
var $table = $('#table');
    $(function () {
        $('#toolbar').find('select').change(function () {
            $table.bootstrapTable('refreshOptions', {
                exportDataType: $(this).val()
            });
        });
    })

        var trBoldBlue = $("table");

    $(trBoldBlue).on("click", "tr", function (){
            $(this).toggleClass("bold-blue");
    });


var $table_empty = $('#table');
    $(function removeTableBody() {
        $('#table tbody').empty();
    })
// I wrote the line above to empty the table

当我按下按钮来清空表中的行时,我会在终端上看到以下内容:

代码语言:javascript
运行
复制
[15/Dec/2021 15:34:05] "GET /style.css HTTP/1.1" 404 2283
[15/Dec/2021 15:34:05] "GET /script.js HTTP/1.1" 404 2283

这是表的外观视图:

在这里输入图像描述

每次发送get请求时,我都想得到新的回复。

EN

回答 1

Stack Overflow用户

发布于 2021-12-15 14:40:15

从您的.css和.js报告404未找到这一事实来看,我将假设这是静态根的问题。我认为默认情况下,设置文件中的STATIC_ROOT将是/static,但是您的js显然位于/ /static/web/dist,所以在加载文件时,您应该将STATIC_ROOT更新为/static/web/dist,或者在base.html中指定从/static到/static的路径。

(编辑)也。您的视图是从另一个网站请求数据,并将接收到的所有数据保存为新的模型实例。在页面上获得副本是因为数据库中有副本,而不是因为javascript。

您的javascript设置为每次单击Search时重新加载页面,因此,javascript未能清除表与重复问题肯定无关。

最后,您的removeTableBody函数在$( )中,这意味着该函数只存在于这些括号的范围内。因此,如果您单击clear按钮,我肯定它会将removeTableBody函数不存在发布到您的控制台。删除$( )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70364960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档