锚标记内的django-URL相对于当前页面django

问题描述 投票:0回答:3

urls.py

    path('add_a_product_to_store/<store_id>',views.add_a_product_to_store,name='add_a_product_to_store'),
    path('show_a_store/<store_id>', views.show_a_store,name='show_a_store')

show_a_store.html

<a href="add_a_product_to_store/5">Add a product</a>

当用户输入ip:port/show_a_store/5时。 。 。 。显示show_a_store.html。但是锚标记内的链接指向http://127.0.0.1:8000/show_a_store/add_a_product_to_store/5,而不是http://127.0.0.1:8000/add_a_product_to_store/5

无论当前页面如何如何使其指向实际URL?

html django anchor
3个回答
2
投票

如下所示添加斜线

path('add_a_product_to_store/<store_id>/',views.add_a_product_to_store,name='add_a_product_to_store'),
path('show_a_store/<store_id>/', views.show_a_store,name='show_a_store')

和模板

<a href="{% url 'add_a_product_to_store' store_id=object.id %}">Add a product</a>

1
投票

您需要使用url模板标签

<a href="{% 'add_a_product_to_store' store_id=5 %}">Add a product</a>

0
投票

感谢您的回答。我在html中犯了一个错误。使用锚标记时,<a href="foo/">表示**ip:port/url-of-current-page/foo/...<a href="/foo/">表示ip:port/foo/如果URL的开头没有forward slash (/),则将其视为相对URL,并将当前页面的URL附加到URL。

© www.soinside.com 2019 - 2024. All rights reserved.