【发布时间】:2021-07-05 10:02:11
【问题描述】:
我的网址被定向到错误的位置。 href 在 search.html 中使用时会将我引导到正确的位置,但是当我在 categories.html 中使用相同的 url 时,它会给出错误位置的错误页面。
文件categories.html
<p class="card-text"><a href="results/detail/" class="btn btn-sm btn-primary">{{news.title}}</a></p>
我必须被重定向到http://127.0.0.1:8000/newspaperapp/results/detail/1,但不是这样,而是将我重定向到http://127.0.0.1:8000/newspaperapp/category/results/detail/。
这是我的urls.py
from django.urls import path
app_name = 'newspaperapp'
from django.conf import settings
from . import views
from .views import SearchView
urlpatterns=[
path('',views.home,name='home'),
path('results/', SearchView.as_view(), name='search'),
path('results/detail/<int:id>',views.detail,name='detail'),
path('category/<int:id>',views.category,name='category'),
]
【问题讨论】:
-
首先,您没有与此 url
href="results/detail/"匹配的模式。其次,不以/开头的相对 URL 意味着它们与当前页面相对,如果它们以/开头,则它们来自域。所以它应该是href="/results/detail/"(由于这个url中没有id或数字,它仍然会出错)。
标签: html django django-views django-templates django-urls