【问题标题】:django-URL inside anchor tag becomes relative to the current page django锚标记内的 django-URL 变为相对于当前页面 django
【发布时间】:2020-03-25 07:24:16
【问题描述】:

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


    【解决方案1】:

    如下图添加斜线

    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>
    

    【讨论】:

      【解决方案2】:

      你需要使用url模板标签

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2023-03-24
          • 2022-01-22
          • 2011-11-19
          • 2021-05-07
          • 1970-01-01
          • 2021-03-22
          • 2018-02-17
          • 2014-05-26
          • 2010-10-19
          相关资源
          最近更新 更多