为了账号安全,请及时绑定邮箱和手机立即绑定

Django链接表行查看

Django链接表行查看

qq_笑_17 2024-01-22 14:30:35
编码新手。我正在使用 django 框架创建一个库存表。我希望能够单击表行(这是一部分),然后将该行/部分信息解析为详细视图。据我了解,表行是内联元素,内联元素是伪代码,当前的 HTML 不允许抛出锚标记(不好的做法?),所以我需要使用一些 javascript。目前,库存视图显示当我使用 runserver ( http://127.0.0.1:8000/inventory/ ) 但单击任何行时不会执行任何操作。这是我到目前为止所拥有的;库存.html{% extends "base.html" %}{% block body %}<br><table class="table table-hover">    <thead>    <tr>        <th>Part Number</th>        <th>Description</th>        <th>Location</th>        <th>Supplier</th>        <th>S.O.H</th>    </tr>    </thead>    <tbody>    {% for part in parts %}    <!-- need to make these table rows link to their respective parts    class="table_row" href="{{ selected_part.partnumber }}/detail">{{ selected_part.partnumber }}-->    <tr data-href="{% url 'detail' part.pk %}">        <td>{{ part.pk }}</td>        <td>{{ part.partnumber }}</td>        <td>{{ part.description }}</td>        <td>{{ part.location }}</td>        <td>{{ part.supplier }}</td>        <td>{{ part.stockonhand }}</td>    </tr>    {% endfor %}    </tbody></table>{% endblock %}urls.pyfrom django.urls import pathfrom .views import *urlpatterns = [    path('inventory/', inventory_list, name='inventory'),  # URL path for inventory_list view    path('<str:pk>/', part_information, name='detail'),    path('', index, name='index'),]自定义.js$('tr[data-href]').on("click", function() {    document.location = $(this).data('href');});base.html 在标签<script src="/docs/4.4/dist/js/custom.js"></script>之前</body>。我认为问题出在我的 javascript 文件中。我对此很陌生,非常感谢简化的解释
查看完整描述

1 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

在您的 custom.js 文件中使用以下内容。当页面加载时,此函数 $(document).ready() 被执行,初始化 tr 的“点击时”操作


    $(document).ready(function(){

      $('table tr').click(function(){

        window.location = $(this).data('href');

        return false;

      });

    });


查看完整回答
反对 回复 2024-01-22
  • 1 回答
  • 0 关注
  • 25 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信