Django链接表行查看

编码新手。我正在使用 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.py


from django.urls import path

from .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 文件中。我对此很陌生,非常感谢简化的解释


qq_笑_17
浏览 29回答 1
1回答

料青山看我应如是

在您的 custom.js 文件中使用以下内容。当页面加载时,此函数 $(document).ready() 被执行,初始化 tr 的“点击时”操作&nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; &nbsp; $('table tr').click(function(){&nbsp; &nbsp; &nbsp; &nbsp; window.location = $(this).data('href');&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5