【问题标题】:How do I change the URL in Django framework如何更改 Django 框架中的 URL
【发布时间】:2021-11-23 04:36:18
【问题描述】:

我正在尝试使用 Django 创建一个网站,但收到一些类似这样的错误消息

Request URL:    http://127.0.0.1:8000/store.html

Using the URLconf defined in greatkart.urls, Django tried these URL patterns, in this order:

    admin/
    [name='home']
    store/
    ^media/(?P<path>.*)$

The current path, store.html, didn't match any of these.

问题是当我尝试单击按钮时,它总是打印 ./store.html 而不是 ./store 这是我的按钮的 html 代码

<a href="./store.html" class="btn btn-outline-primary float-right">See all</a>

这是我的 Django 的 urls.py (main)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('store/', include("store.urls"))
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

这个 urls.py(store)

urlpatterns = [
    path('', views.store, name='store'),
    path('<slug:category_slug>/', views.store, name='product_by_category'),
]

views.py

def store(request, category_slug = None):
    return render(request, 'store/store.html', context)

有人知道吗?我想在不更改 HTML 代码的情况下修复它,因为我已经尝试过了,当我两次单击按钮时会出现一些错误,因为 URL 会打印 ./store 两次

【问题讨论】:

    标签: python python-3.x django django-views http-status-code-404


    【解决方案1】:

    这里的 url 应该是绝对的(以 / 开头,不包括您的本地地址)并且不应包括 .html。 所以你的网址应该是: /store 并且锚标记应该是: &lt;a href="/store" class="btn btn-outline-primary float-right"&gt;See all&lt;/a&gt;

    【讨论】:

    • 哦,我看到了我包含的问题 ./store 而不是 /store,所以每当我单击按钮时它都会打印 /store,谢谢您的帮助
    • @MarcusAurelius ,我很高兴它有所帮助。考虑支持答案并将其作为解决方案接受。请:)
    猜你喜欢
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2020-03-22
    相关资源
    最近更新 更多