Home > Software engineering >  Django - Combining Urls from different apps
Django - Combining Urls from different apps

Time:10-26

I am fairly new to Django and I had a question regarding how to manage multiple django apps. I have an app that deals with user accounts which shows the profile page and dashboard of the user. However, I have another app called blogs where I want to show the blogs written by that user.

What I basically want is to have a separate tab in the navbar that says "blogs" and when you click on the blogs it should go to the url "localhost:8000/user/blogs" not just localhost:8000/blogs. How can I go about combining the two apps in such a way?

CodePudding user response:

in your "root" urls.py:

from django.urls import path, include

urlpatterns = [
    ...
    path('users/', include(('usersapp.urls', 'usersapp'), namespace='usersapp')),
    ...
]

In your users app urls.py:

from django.urls import path, include

urlpatterns = [
    ...
    path('blogs/', include(('blogsapp.urls', 'blogsapp'), namespace='blogsapp')),
    ...
]

And now in your templates you have to do:

{% url '<namespace>:<urlname>' %}

for example:

{% url 'blogsapp:index' %}
  • Related