在djangopython中显示数据更改后的重复数据并刷新页面

mwecs4sa  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(124)

我只想在本地主机上显示一次数据,因为它会出现两次,无论我在哪里更改data.py并刷新页面,都会添加新的数据层。
所以如果数据

titles = {
    "data": [
        {
            "sid": "1234",
            "name": "name_1"
        },
        {
            "sid": "5678",
            "name": "name_2"
        },
        {
            "sid": "9012",
            "name": "name_3"
        }
    ]
}

则它应在输出中显示为

SD      Name
1234    name_1
5678    name_2
9012    name_3

如果我更改www.example.com文件中的数据data.py

titles = {
    "data": [
        {
            "sid": "0000",
            "name": "name_1"
        },
        {
            "sid": "12313",
            "name": "name_2"
        },
        {
            "sid": "543534",
            "name": "name_3"
        }
    ]
}

输出应如下所示

SD      Name
0000    name_1
12313   name_2
543534  name_3

电流输出为

SD      Name
1234    name_1
5678    name_2
9012    name_3
0000    name_1
12313   name_2
543534  name_3

输出是添加以前的记录。我想要的是刷新后它应该出现在www.example.com文件中的当前数据data.py
views.py

from django.shortcuts import render
from .models import Title

def get(request):
    context = {'titles': Title.objects.all()}
    return render(request, "home.html", context)

home.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>TableView - Startup</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2 class="text-center"><u>Data</u></h2><br>
  <table class="table table-dark table-striped">
    <thead>
      <tr>
        <th>SD</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>
    {% for title in titles %}
      <tr>
        <td>{{title.sd}}</td>
        <td>{{title.name}}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</div>

</body>
</html>

models.py

from django.db import models
from data import titles

class Title(models.Model):
    sd = models.CharField(max_length=255)
    name = models.CharField(max_length=255)  # Read the JSON

# Create a Django model object for each object in the JSON
for title in titles['data']:
    Title.objects.create(sd=title['sid'], name=title['name'])

data.py

titles = {
    "data": [
        {
            "sid": "0000",
            "name": "name_1"
        },
        {
            "sid": "12313",
            "name": "name_2"
        },
        {
            "sid": "543534",
            "name": "name_3"
        }
    ]
}
``
bkhjykvo

bkhjykvo1#

我通过在www.example.com中添加以下代码解决了此views.py

def getting_all_data():
for title in titles['data']:
    Title.objects.create(sd=title['sid'], name=title['name'])

def get(request):
    if list(Title.objects.all()):
        Title.objects.all().delete()
        getting_all_data()
        object_handler = Title.objects.all()
    else:
        getting_all_data()
        object_handler = Title.objects.all()
    context = {'titles': object_handler}
    return render(request, "home.html", context)

相关问题