Django urls href redirect bug

Viewed 48

When I click on the "apropos" button on my navbar to access to my "apropos" pages it load the page correctly but with this url http://127.0.0.1:8000/contact/apropos/ when it should be http://127.0.0.1:8000/apropos/.

it does the samething when I click on the "contact" button on my navbar it redirect me to this urls http://127.0.0.1:8000/contact/contact

And when I click on "Accueil" it redirect me to http://127.0.0.1:8000/contact When it should redirect me to 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 (navbar section)

<!-- 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 -->


1 Answers

I think there are two issues that might be causing this. One, your root urls.py should be like this:

urlpatterns = [
    path("", include("main.urls")),    
    path('admin/', admin.site.urls),
]

Why? the main urls.py will be appended to the ones in your root, and you should not have multiple include("main.urls") for the same path.

Second, you need to put single quotes in the anchor links, for example:

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

Why? Because otherwise the double quotes would end after url, and then begin again after home.

Related