Django urls href 重定向错误
Django urls href redirect bug
当我点击导航栏上的“apropos”按钮访问我的“apropos”页面时,它正确地加载了页面,但是这个 url http://127.0.0.1:8000/contact/apropos/
应该是 http://127.0.0.1:8000/apropos/
.
当我点击导航栏上的“联系人”按钮时,它会做同样的事情,它会将我重定向到这个 urls http://127.0.0.1:8000/contact/contact
当我点击“Accueil”时,它会将我重定向到 http://127.0.0.1:8000/contact
什么时候应该将我重定向到 http://127.0.0.1:8000/
mysite/urls.py
urlpatterns = [
path("", include("main.urls")),
path('admin/', admin.site.urls),
path("apropos/", include("main.urls")),
path("contact/", include("main.urls")),
]
main/urls.py
urlpatterns = [
path("", views.home, name="home"),
path("apropos/", views.apropos, name="apropos"),
path("contact/", views.contact, name="contact")
]
views.py
def home(response):
return render(response, "main/index.html", {})
def apropos(response):
return render(response, "main/about.html", {})
def contact(response):
return render(response, "main/contact.html", {})
index.html(导航栏部分)
<!-- header nav section -->
<header class="xs-header-nav">
<div class="container">
<div class="row menu-item">
<div class="col-lg-12">
<nav id="navigation1" class="navigation header-nav clearfix">
<div class="nav-header">
<!-- <a class="nav-brand" href="#"></a>-->
<a href="./index.html" class="mobile-logo">
<img src="static/assets/images/mobile_logo.png" alt="">
</a>
<div class="nav-toggle"></div>
</div>
<div class="nav-menus-wrapper clearfix">
<ul class="nav-menu">
<li class="active"><a href="{% url "home" %}">Home</a>
</li>
<li><a href="#">Services</a>
</li>
<li><a href="gallery.html">Portfolio</a></li>
<li><a href="{% url "apropos" %}">À-propos</a></li>
<li>
<a href="{% url "contact" %}">Contact</a>
</li>
</ul>
<div class="header-nav-right-info align-to-right">
<label><i class="icon icon-phone3"></i> (514) 569-2380</label>
</div>
</div>
</nav>
</div>
</div><!-- .row end -->
</div><!-- .container end -->
</header><!-- End header nav section -->
我认为有两个问题可能会导致此问题。一,你的根urls.py应该是这样的:
urlpatterns = [
path("", include("main.urls")),
path('admin/', admin.site.urls),
]
为什么?主 urls.py 将附加到根目录中的那些,并且同一路径不应有多个 include("main.urls")
。
其次,您需要在锚链接中加上单引号,例如:
<a href="{% url 'home' %}">Home</a>
为什么?因为否则双引号将在 url 之后结束,然后在 home.
之后再次开始
当我点击导航栏上的“apropos”按钮访问我的“apropos”页面时,它正确地加载了页面,但是这个 url http://127.0.0.1:8000/contact/apropos/
应该是 http://127.0.0.1:8000/apropos/
.
当我点击导航栏上的“联系人”按钮时,它会做同样的事情,它会将我重定向到这个 urls http://127.0.0.1:8000/contact/contact
当我点击“Accueil”时,它会将我重定向到 http://127.0.0.1:8000/contact
什么时候应该将我重定向到 http://127.0.0.1:8000/
mysite/urls.py
urlpatterns = [
path("", include("main.urls")),
path('admin/', admin.site.urls),
path("apropos/", include("main.urls")),
path("contact/", include("main.urls")),
]
main/urls.py
urlpatterns = [
path("", views.home, name="home"),
path("apropos/", views.apropos, name="apropos"),
path("contact/", views.contact, name="contact")
]
views.py
def home(response):
return render(response, "main/index.html", {})
def apropos(response):
return render(response, "main/about.html", {})
def contact(response):
return render(response, "main/contact.html", {})
index.html(导航栏部分)
<!-- header nav section -->
<header class="xs-header-nav">
<div class="container">
<div class="row menu-item">
<div class="col-lg-12">
<nav id="navigation1" class="navigation header-nav clearfix">
<div class="nav-header">
<!-- <a class="nav-brand" href="#"></a>-->
<a href="./index.html" class="mobile-logo">
<img src="static/assets/images/mobile_logo.png" alt="">
</a>
<div class="nav-toggle"></div>
</div>
<div class="nav-menus-wrapper clearfix">
<ul class="nav-menu">
<li class="active"><a href="{% url "home" %}">Home</a>
</li>
<li><a href="#">Services</a>
</li>
<li><a href="gallery.html">Portfolio</a></li>
<li><a href="{% url "apropos" %}">À-propos</a></li>
<li>
<a href="{% url "contact" %}">Contact</a>
</li>
</ul>
<div class="header-nav-right-info align-to-right">
<label><i class="icon icon-phone3"></i> (514) 569-2380</label>
</div>
</div>
</nav>
</div>
</div><!-- .row end -->
</div><!-- .container end -->
</header><!-- End header nav section -->
我认为有两个问题可能会导致此问题。一,你的根urls.py应该是这样的:
urlpatterns = [
path("", include("main.urls")),
path('admin/', admin.site.urls),
]
为什么?主 urls.py 将附加到根目录中的那些,并且同一路径不应有多个 include("main.urls")
。
其次,您需要在锚链接中加上单引号,例如:
<a href="{% url 'home' %}">Home</a>
为什么?因为否则双引号将在 url 之后结束,然后在 home.
之后再次开始