【问题标题】:Django - links are incorrectly resolved in menuDjango - 菜单中的链接解析不正确
【发布时间】:2022-01-23 16:20:19
【问题描述】:

我正在本地开发 Django 页面,但突出显示的菜单存在一些问题。

  • 将鼠标悬停在“moje projekty”上时,我会看到以下链接

127.0.0.1:8080/portfolio/,我点击打开页面

  • 当我第二次悬停时,它显示:

127.0.0.1:8080/portfolio/portfolio/,我点击打开页面

  • 现在我第三次悬停时,它显示:

127.0.0.1:8080/portfolio/portfolio/portfolio/,我点击,错误是:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8080/portfolio/portfolio/portfolio/
Using the URLconf defined in my_site.urls, Django tried these URL patterns, in this order:

admin/
[name='home']
about_me/ [name='aboutme']
portfolio/ [name='portfolio']
posts/<slug:the_slug>/ [name='post_detail']
summernote/
^media/(?P<path>.*)$
portfolio/ [name='home']
portfolio/ about_me/ [name='aboutme']
portfolio/ portfolio/ [name='portfolio']
portfolio/ posts/<slug:the_slug>/ [name='post_detail']
portfolio/ summernote/
portfolio/ ^media/(?P<path>.*)$
about_me/
The current path, portfolio/portfolio/portfolio/, didn’t match any of these.

base.html:

<!DOCTYPE html>
<html>

    <head>
        <title>Moja strona</title>
         <link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet">
        <meta name="google" content="notranslate" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
            crossorigin="anonymous" />
    </head>

    <body>
        <style>
            body {
            font-family: "Roboto", sans-serif;
            font-size: 17px;
            background-color: #fdfdfd;
        }
        .shadow {
            box-shadow: 0 4px 2px -2px rgba(0, 0, 0, 0.1);
        }
        .btn-danger {
            color: #fff;
            background-color: #f00000;
            border-color: #dc281e;
        }
        .masthead {
            background: #3398E1;
            height: auto;
            padding-bottom: 15px;
            box-shadow: 0 16px 48px #E3E7EB;
            padding-top: 10px;
        }
        img {
            width: 00%;
            height: auto;
            object-fit: contain; 
    </style>
        {% load static %}
               <center> <img src="{% static 'blog/moj.png' %}" alt="My image"> </center>

        <!-- Navigation -->
        <nav class="navbar navbar-expand-sm navbar-light bg-light shadow" id="mainNav">
            <div class="container-fluid">
                
                
                <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive"
                    aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item text-black">
                            <a class="nav-link text-black font-weight-bold" href="/">Blog</a>
                        </li>
                        <li class="nav-item text-black">
                            <a class="nav-link text-black font-weight-bold" href="portfolio/">Moje projekty</a>
                        </li>
                        <li class="nav-item text-black">
                            <a class="nav-link text-black font-weight-bold" href="about_me">Kontakt</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        {% block content %}
        <!-- Content Goes here -->
        {% endblock content %}
        <!-- Footer -->

    </body>
</html>

urls.py 项目:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
     path('portfolio/', include('blog.urls')),
     path('about_me/', include('blog.urls')),
    ]

urls.py 应用程序:

from . import views
from django.urls import path
from django.conf.urls import include

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('about_me/', views.Aboutme.as_view(), name='aboutme'),
    path('portfolio/', views.Portfolio.as_view(), name='portfolio'),
   
    path('posts/<slug:the_slug>/', views.PostDetail.as_view(), name='post_detail'),
    path('summernote/', include('django_summernote.urls')),


    ]
# to jest dla wysiwyg
  # add condition in django urls file

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

问题:

  • 为什么第二次点击时链接会改变?
  • 为什么我在第三次点击后收到此错误?

【问题讨论】:

  • 它附加到当前 url 因为href="portfolio/" 是一个相对 url。

标签: django django-templates django-urls


【解决方案1】:

嘿 :) 路径 /portfolio/ 和 /portfolio/portfolio/ 是正确的,但是当您使用不带正斜杠的 htm href='portfolio/' 时,它们不是绝对的。 html 只需将其添加到现有的。因此,如果您在 example.com/portfolio/ 中单击此 href,您将添加另一个“portfolio/”。

只在 href 中使用:

 <a class="nav-link text-black font-weight-bold" href="{% url 'blog:portfolio' %}">

PS。在项目的 urls.py 中,您不需要指向同一个应用程序的两条路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    相关资源
    最近更新 更多