【问题标题】:How to execute a django or SQL query after clicking the toggle button?单击切换按钮后如何执行 django 或 SQL 查询?
【发布时间】:2021-12-30 04:48:03
【问题描述】:

我有两张桌子:

class DibbsSpiderDibbsMatchedProductFieldsDuplicate(models.Model):
    nsn = models.TextField()
    nsn2 = models.TextField()
    cage = models.TextField()
    part_number = models.TextField()
    company_name = models.TextField(blank=True, null=True)
    supplier = models.TextField(db_column='Supplier', blank=True, null=True)  # Field name made lowercase.
    cost = models.CharField(db_column='Cost', max_length=15, blank=True, null=True)  # Field name made lowercase.
    list_price = models.CharField(db_column='List_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    gsa_price = models.CharField(db_column='GSA_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    hash = models.TextField()
    nomenclature = models.TextField()
    technical_documents = models.TextField()
    solicitation = models.CharField(max_length=32)
    status = models.CharField(max_length=16)
    purchase_request = models.TextField()
    issued = models.DateField()
    return_by = models.DateField()
    file = models.TextField()
    vendor_part_number = models.TextField()
    manufacturer_name = models.TextField(blank=True, null=True)
    product_name = models.TextField(blank=True, null=True)
    unit = models.CharField(max_length=15, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'dibbs_spider_dibbs_matched_product_fields_duplicate'


class DibbsSpiderSolicitation(models.Model):
    line_items = models.IntegerField()
    nsn = models.TextField()
    nomenclature = models.TextField()
    technical_documents = models.TextField()
    purchase_request = models.TextField()

    class Meta:
        managed = False
        db_table = 'dibbs_spider_solicitation'

我想以以下格式显示这些:

原文链接在:http://develop-330.gsa-cs.com/test/

点击切换按钮后,如何执行查询?

第 1 行表示第 line_items 行

第一行是第一个表。

点击切换后,我想执行一个 django 查询或一个连接两个表的 sql 查询,因为它们有共同的 nsn。

nsn 在两个表中都很常见。

如果有更多行项目,则应重复连接的查询。

按钮代码:

<td onclick="showHideRow('switch_1');">
                        <button type="button" class="btn btn-default" data-toggle="collapse"></button>
                     </td>

【问题讨论】:

  • 你指的是哪个切换按钮?
  • 有一个按钮我已经粘贴了代码
  • 我不明白点击按钮后你想加入表格然后?
  • 并显示连接的表。

标签: python django ajax django-views django-templates


【解决方案1】:

您不能在 html 文件中执行 sql 查询。如果你想这样做,你可以在urls.py中添加一个新的url,在views.py中添加一个函数来发送连接表查询,然后返回结果。

您可以通过ajax GET request 来执行此操作(在ajax 成功函数中,您可以添加或替换从view.py 接收到的数据为html 中的当前数据)在jQuery 中,您可以等待切换事件,点击切换后按钮,您可以向 url 发送 ajax 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2018-07-12
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多