編碼新手。我正在使用 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 文件中。我對此很陌生,非常感謝簡化的解釋
django鏈接表行查看
夢里花落0921
2022-07-08 18:22:36