简体   繁体   中英

# replacing url template tag in django

On my home page I have my navbar and in my navbar i have a link for the about page and use the template url tag inside a href which looks like this

<a href="{% url 'about' %}" class="nav-link">About</a>

But when i run my project and view it in a preview window it does not work. When clicked on a # key appears in the url where '/about' should appear. when i use googles dev tools to look at the element # is inside the href instead of the template url tag and when i edit the html in google dev tools and change it to '/about/' the link works and takes me to the about page and i dont know why this is happening

here is the code in my /about/urls.py

from django.urls import path
from . import views

urlpatterns = [
   path('', views.about, name='about'),
]

and here is the code inside my /about/views.py

  from django.shortcuts import render
  from django.http import HttpResponse

  def about(request):
      return render(request, 'about/about.html')

any help is much appreciated and thank you in advance

Your home page is located in a different app as the urls.py where the url you need is defined. You combine all the different url paths in the main app's urls.py: SeanKellyQuarry/SeanKellyQuarry/urls.py . Here, you should assign a namespace to each, so that Django can know what url you're referring to with no ambiguity:

urlpatterns = [
    path('', include('home.urls')),
    path('admin/', admin.site.urls),
    path('about/', include('about.urls'), namespace="about"),  # <==
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

At this point, to refer to any named url within about.urls, you need to prepend the namespace. So if about.urls looks like:

urlpatterns = [
    path('', views.about, name='about'),
]

Then in templates you need to use:

<a href="{% url 'about.about' %}" class="nav-link">About</a>

The first about corresponds to the namespace you define in the main urls.py, the second one matches the name of the url in the included urls.py.

However , do note that you introduce a lot of overhead and unnecessary complexity by having one app (and so one urls.py, one views.py...) for each page, especially for pages that (presumably) will be static, with no need for models or complex views.
I recommend keeping your main, static content in one app and only make separate ones for bigger units of complex functionality (eg group all "Blog"-related functionality, pages, models, urls and everything, in its own app separate from the static pages, then another separate app for "To-do list" functionality...).

You have to set app_name in /about/urls.py :

from django.urls import path
from . import views

app_name = "about"

urlpatterns = [
   path('', views.about, name='about'),
]

Use app_name:urlname at template:

<a href="{% url 'about:about' %}" class="nav-link">About</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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