简体   繁体   English

<a>标签中的href在内部不起作用</a><li> <a>DJANGO</a></li>

[英]href in <a> tag not working inside <li> DJANGO

href in a tag inside li don't woriking. li 内部标签中的 href 不起作用。 It does not redirect me to /sub1/.它不会将我重定向到 /sub1/。 This is my code:这是我的代码:

 body, html { margin: 0; font-family: Arial, sans-serif; font-size: 1em; }.navbar { width: 100%; background-color: #4682B4; height: 80px; color: #fff; }.navbar.nav { margin: 0; padding: 0; float: left; list-style-type: none; }.navbar.nav li.nav-item { float: left; margin: 0 95px; }.navbar.nav li.nav-item>a { display: inline-block; padding: 15px 20px; height: 50px; line-height: 50px; color: #fff; text-decoration: none; }.navbar.nav li.nav-item>a:hover { background-color: #B0C4DE; }.dropdown a:focus { background-color: #B0C4DE; }.dropdown a:focus~.dropdown-container { max-height: 500px; transition: max-height 0.5s ease-in; -webkit-transition: max-height 0.5s ease-in; -moz-transition: max-height 0.5s ease-in; }.dropdown-container { position: absolute; top: 80px; max-height: 0; overflow: hidden; background: #4682B4; color: #B0C4DE; }.dropdown-container a { color: #fff; text-decoration: none; }.dropdown-menu { margin: 0; padding: 0; list-style-type: none; }.dropdown-menu li a { display: inline-block; min-width: 250px; padding: 15px 20px; border-bottom: 1px solid #333; }.dropdown-menu li a:hover { text-decoration: none; background: #B0C4DE; }
 <body> <nav class="navbar"> <ul class="nav" id="primary-nav"> <li class="nav-item"><a href="/index/">Home</a></li> <li class="nav-item dropdown"> <a href="#">Servicios</a> <div class="dropdown-container"> <div class="dropdown-inner"> <ul class="dropdown-menu"> <li class="dropdown-menu-item"><a href="{% url 'Datos:sub1' %}">Submenu 1</a></li> <li class="dropdown-menu-item"><a href="#">Submenu2</a></li> </ul> </div> </div> </li> <li class="nav-item"><a href="">Agenda</a></li> <li class="nav-item"><a href="">¿Quiénes somos?</a></li> <li class="nav-item"><a href="/contact/">Contacto</a></li> </ul> </nav> </body>

Urls.py网址.py

from django.urls import path, re_path
from Applications.Datos import views

app_name = "Datos"

urlpatterns = [
    re_path('^sub1/', views.Datos, name='sub1'),
    path('search/', views.buscar),
]

Views.py视图.py

from django.shortcuts import render
from django.http import request

# Create your views here.
def Datos(request):
    return render(request, template_name='datos.html')

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

You are not using correct syntax.您没有使用正确的语法。

<a href="{% url '<app_name>:<view_name>' %}"></a>

Where <view_name> is the view name you gave in urls.py for the app in which template is made.其中 <view_name> 是您在 urls.py 中为制作模板的应用程序提供的视图名称。 like here:像这儿:

from django.urls import path, re_path
from home import views

app_name = "home"
urlpatterns = [
    path('', views.home_view, name='productsPage'),
    re_path('^wishlist/', views.wishlist_view, name='wishlistPage'),
]

The name of the view is wishlistPage and app is home so you write to make a link to that page redirect:视图的名称是wishlistPage并且 app 是home所以你写了一个链接到那个页面重定向:

<a href="{% url 'home:wishlistPage' %}"></a>

Update更新

sub1 is the path of your MAIN VIEW FOR THAT APP when you want to visit <youwebsite>/sub1 . sub1是您想要访问<youwebsite>/sub1时该应用程序的主视图的路径。 In that case the urls.py for this app is incorrect.在这种情况下,此应用程序的 urls.py 不正确。 Edit this urls.py to be like this:编辑这个urls.py是这样的:

from django.urls import path, re_path
from Applications.Datos import views

app_name = "Datos"

urlpatterns = [
    re_path('', views.Datos, name='sub1'),  # UPDATE: LET IT BE BLANK
    path('search/', views.buscar),
]

And update your main urls.py to b like this:并将您的主要urls.py更新为 b ,如下所示:

urlpatterns = [
    [...your other urls...],
    re_path(r'^sub1/', include(('Datos.urls', 'Datos'), namespace='Datos')),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM