Home > Enterprise >  Django urls href redirect bug
Django urls href redirect bug

Time:01-24

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 >
    <div >
        <div >
            <div >
                <nav id="navigation1" >

                    <div >
                        <!--  <a  href="#"></a>-->
                        <a href="./index.html" >
                            <img src="static/assets/images/mobile_logo.png" alt="">
                        </a>
                        <div ></div>
                    </div>

                    <div >
                        <ul >
                <li ><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 >
                            <label><i ></i> (514) 569-2380</label>
                        </div>
                        
                    </div>

                </nav>
            </div>
        </div><!-- .row end -->
    </div><!-- .container end -->
</header><!-- End header nav section -->


CodePudding user response:

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.

  •  Tags:  
  • Related