django-URL inside anchor 标签变成相对于当前页面django
django-URL inside anchor tag becomes relative to the current page django
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
显示。
但是锚标记内的 link 指向 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 而不管当前页面?
如下所示添加斜杠
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>
您需要使用 url 模板标签
<a href="{% 'add_a_product_to_store' store_id=5 %}">Add a product</a>
感谢您的回答。
我在 html 中犯了一个错误。
使用锚标签时,<a href="foo/">
表示**ip:port/url-of-current-page/foo/...
但
<a href="/foo/">
意思是 ip:port/foo/
如果url开头没有forward slash (/)
,则视为亲戚url,将当前页的url附加到url。
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
显示。
但是锚标记内的 link 指向 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 而不管当前页面?
如下所示添加斜杠
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>
您需要使用 url 模板标签
<a href="{% 'add_a_product_to_store' store_id=5 %}">Add a product</a>
感谢您的回答。
我在 html 中犯了一个错误。
使用锚标签时,<a href="foo/">
表示**ip:port/url-of-current-page/foo/...
但
<a href="/foo/">
意思是 ip:port/foo/
如果url开头没有forward slash (/)
,则视为亲戚url,将当前页的url附加到url。