【问题标题】:how to dynamically update html table in django如何在 django 中动态更新 html 表
【发布时间】:2021-11-19 11:28:47
【问题描述】:

如何动态更新 HTML 表中主机名列下的搜索内容。 每次都需要更新搜索内容,根据用户输入的主机名数量相应增加行号。

这是我的 index.html

{% extends 'base.html' %}
    
    
    
    {% block title %} IP Finder {% endblock %}
    
    
    {% block body %}

<body>
<div>

        {% csrf_token %}

        <div class="form-group">

            <label>

                <input type="text" class="form-control" name="search" placeholder="Enter website" autocomplete="off">
            </label>

        <input type="submit" class="btn btn-primary" value="Search">
</div>

<div>
<div id="section2">
<center>
<table class = "a">
  <tr>
    <th>ID</th>
    <th>Hostname</th>
    <th>IP Address(IPv4)</th>
    <th>IP Address(IPv6)</th>
    <th>Port 1</th>
    <th>Port 2</th>
  </tr>
  <tr>
    <td>1</td>
    <td>{{ hostname }}</td>
    <td>{{ ipv4 }}</td>
    <td>{{ ipv6 }}</td>
    <td>{{ port1 }}</td>
    <td></td>
  </tr>

这是我的观点.py

from django.shortcuts import render
import dns
import dns.resolver
import socket
import sys


def index(request):
    if request.method == 'POST':

        search = request.POST.get('search')
        # search = 'www.google.com'
        # search = "'" + search + "'"
        ip_address = dns.resolver.Resolver()
        IPv4 = ip_address.resolve(search, 'A').rrset[0].to_text()
        IPv6 = ip_address.resolve(search, 'AAAA').rrset[0].to_text()
        return render(request, 'index.html', {"ipv4": IPv4, "ipv6": IPv6, "hostname": search})

【问题讨论】:

    标签: javascript python html django ajax


    【解决方案1】:

    您需要 1) 实现 Ajax 2) 或者预加载所有表数据,然后使用 Datatables.net [0] 来实现动态搜索。

    [0]https://datatables.net/

    【讨论】:

    • 感谢您的帮助。我想用用户输入的内容更新主机名列。我是 web 开发平台的新手,我不知道如何实现 ajax 代码你能帮我解决同样的问题吗/
    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    相关资源
    最近更新 更多