Django doubles path of link (wrong urls.py configurations?)

Hi Everyone,

I am currently working on a website. Up in the Navigation bar are several links
like “Home” “Contact” etc. One of my links is called “Teilnehmerliste”.
If I now click on “Teilnehmerliste” in the navigation bar, at first I get to my page successfully. But if I then want to click another link, Django doubles my path. For example, when I click “Contact”, Django calls the following link:
“WEBSITENAME/teilnehmerliste/contact” instead of “WEBSITENAME/contact”. And from then on nothing works anymore (Page not found). My HTML file for the link (Teilnehmerliste) is called “teilnehmerliste.html” and is located in the subfolder “teilnehmerliste” which in turn is in the “templates” folder. And the app folder is called “manage_teilnehmerlisteliste_app”.

My current configurations look like this:

urls.py (from the project folder):

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

urlpatterns = [
    path('teilnehmerliste/', include('manage_teilnehmerliste_app.urls')),
    
    ]

urls.py (from the app folder):

from django.urls import path
from . import views

urlpatterns = [
    
    path('', views.teilnehmerliste, name='teilnehmerliste'),

views.py (from the app folder):

from django.shortcuts import render, redirect
from .models import ManageTeilnehmerListe

# Create your views here.

def teilnehmerliste (request):
    context = ManageTeilnehmerListe.objects.all()
    

    if request.method == 'POST':
        new_participant = ManageTeilnehmerListe(
            title = request.POST['title']

        )
        new_participant.save() 
        return redirect('/teilnehmerliste')

    return render(request, 'teilnehmerliste/teilnehmerliste.html', {'contexts': context})

settings.py Datei:

TEMPLATES = [
    {
	...
        'DIRS': [BASE_DIR/'templates'],
	...
]

INSTALLED_APPS = [
    ...
    'manage_teilnehmerliste_app',
    ...
]

models.py (aus dem App-Ordner):

from django.db import models

class ManageTeilnehmerListe(models.Model):
    title = models.CharField(max_length=50)

    def __str__(self):
        return self.title

PS: By the way, I’ve already looked at many sites that deal with the problem.

Where is the mistake?

This is almost certainly a template error.

Please post the template that is used to create the page creating the link with the wrong url being generated.

here is the HTML-Part from the html-template. I guess the CSS-Part is not important:

<body id="ixck">
    <header id="irzc" class="header-banner">
      <div id="iyxg" class="container-width">
        <div id="i1ei" class="logo-container">
        </div>
        <div class="clearfix">
        </div>
        <div id="is2u1" class="row">
          <div id="i29jk" class="cell">
            <div id="il0f" class="logo">
              <span id="i3pqk">Testsite 123</span>
              <br/>
            </div>
          </div>
        </div>
        <div id="iiwyf">
          <span></span>
        </div>
        <div id="ioufd">
          <i>connecting &amp; expanding through music...
          </i>
          <br/>
        </div>
        <div id="ipkxf" class="row">
          <div id="imkmk" class="cell">
            <div id="iftzx" class="menu-item">
              <a href="" id="izskm">Home</a>
              <br/>
            </div>
            <div id="i8mxq" class="menu-item">
              <a href="teilnehmerliste" id="i9flg">Teilnehmerliste</a>
            </div>
            <div id="imvce" class="menu-item">
              <span id="i5hie"><a href="location" id="idx6h">Location &amp; Wegbeschreibung</a></span>
              <br/>
              <br/>
            </div>
            <div id="i8ejo" class="menu-item">
              <a href="qrcode" id="i7tyo">QR-Code</a>
            </div>
            <div id="i0nfl" class="menu-item">
              <span id="itjel"><a href="contact" id="ihtxk">Kontakt</a></span>
              <br/>
            </div>
          </div>
        </div>
      </div>
      <div id="iewyh" class="row">
      </div>
      <div id="idjhh" class="row">
        <div id="ikgjr" class="cell">
          <div id="innnd" class="row">
            <div id="i6j6g" class="cell">
              <form action="" id="ilyvqf" method="POST" class="form">
                {% csrf_token %}
                <div class="form-group">
                  <label class="label">Name</label>
                  <input type="text" name="title" placeholder="Type here your name" id="i7kr9a" class="input"/>
                </div>
                <div class="form-group">
                </div>
                <div class="form-group">
                </div>
                <div class="form-group">
                  <button type="submit" id="ijhat9" class="button">Send</button>
                </div>
                <ul id="myUL-2"> 
                  {% for context in contexts reversed %}
                  <li id="i9fgpi">{{context.title | title}} 
                    <span class="close"><a href="delete/{{context.id}}" id="iwd8hq">Delete</a></span>
                  </li>
                  {% endfor %}
                </ul>
              </form>
            </div>
          </div>
        </div>
      </div>
    </header>
    <section id="i3pjs" class="flex-sect">
      <div id="igrwj" class="container-width">
        <div id="ilrt5" class="flex-title">
          <span id="itpjr">Achtung:</span> Die Location für unser gemeinsames 
          <br/>Trommeln
          kann sich manchmal ändern. 
          <br/>Schaue dazu unter 
          <a href="" id="i74xq2">Location &amp; Wegbeschreibung</a>, 
          <br/>um auf dem neusten Stand zu sein!
          <br/>
        </div>
      </div>
    </section>
    <footer id="i2xz7k" class="footer-under">
      <div class="container-width">
        <div id="ittc2a" class="footer-container">
          <div id="izic9p" class="foot-list-item">Home
          </div>
          <div id="izi4et" class="foot-list-item">Teilnehmerliste
          </div>
          <div id="ipffel" class="foot-list-item">QR-Code
          </div>
          <div id="io8gt8" class="foot-list-item">      Location &amp;
            <br id="ij7sk-2" draggable="true"/>Wegbeschreibung
          </div>
          <div id="iq3kb" class="foot-list-item">Kontakt
          </div>
        </div>
      </div>
    </footer>
  </body>

The issue here is that the links you are creating are relative to the current path.

So if your current path is “/xyz”, then this link:

is a reference to “/xyz/contact”.

You would need to make that link “/contact” in order for it to be an absolute url.

However, what you really should be doing in your template is using the url tag and identifying the link by name.

1 Like

Wow! Thanks for the clarification and the solution :wink:
The issue cost me a lot of time and would have done even more…so thanks again.

Adding the “/” to the links in the HTML file now works.

Next I will deal with how to use the URL tags…