Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
Django's URL handling system allows you to perform reverse parsing, which means generating URLs from view function names or URL patterns. Namespaces help avoid naming conflicts when you have multiple apps with similar view names. In this tutorial, we will cover reverse parsing and using namespaces in Django.
myapp/views.py
:from django.http import HttpResponse def goodbye(request): return HttpResponse("Goodbye, World!")
myapp/urls.py
:from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello, name='hello'), path('goodbye/', views.goodbye, name='goodbye'), ]
python manage.py startapp myotherapp
myproject/settings.py
. Add 'myotherapp'
to the INSTALLED_APPS
list:INSTALLED_APPS = [ # ... 'myotherapp', ]
myotherapp/views.py
:from django.http import HttpResponse def goodbye(request): return HttpResponse("Goodbye from myotherapp!")
myotherapp/urls.py
. If this file doesn't exist, create it:from django.urls import path from . import views urlpatterns = [ path('goodbye/', views.goodbye, name='goodbye'), ]
myproject/urls.py
:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include(('myapp.urls', 'myapp'), namespace='myapp')), path('myotherapp/', include(('myotherapp.urls', 'myotherapp'), namespace='myotherapp')), ]
We've included the app name as the second element of the tuple in the include
function, and provided the namespace
argument. This will allow us to differentiate between the two 'goodbye' views in our apps.
hello
view in myapp/views.py
to include a link to the goodbye
view:from django.http import HttpResponse from django.urls import reverse def hello(request): goodbye_url = reverse('myapp:goodbye') html = f"<h1>Hello, World!</h1><p><a href='{goodbye_url}'>Go to Goodbye</a></p>" return HttpResponse(html)
The reverse
function is used to generate a URL for the 'goodbye' view in the 'myapp' namespace. We pass the namespace and the URL pattern name separated by a colon.
python manage.py runserver
This tutorial showed you how to use reverse parsing and namespaces in Django to generate URLs from view names or URL patterns. This can be especially helpful when you have multiple apps with similar view names, as it avoids conflicts and simplifies URL management
Django Reverse URL Parsing and Routing:
# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_home(request): # Redirect to the 'home' URL pattern dynamically home_url = reverse('home') return redirect(home_url)
Django reverse()
Function with Namespaces:
reverse()
function with namespaces to generate URLs for views within specific namespaces.# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_admin(request): # Redirect to the 'admin:index' URL pattern within the 'admin' namespace admin_url = reverse('admin:index') return redirect(admin_url)
Django URL Routing with include()
and Namespaces:
include()
and namespaces to organize URL patterns and route requests to specific views.# urls.py (project-level) from django.urls import path, include urlpatterns = [ path('articles/', include('myapp.urls', namespace='articles')), ] # urls.py (app-level - myapp) from django.urls import path app_name = 'articles' urlpatterns = [ path('', views.article_list, name='article-list'), ]
Django route()
Method for Reverse URL:
route()
method in views to generate URLs for dynamic routing within the app.# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_category(request, category_id): # Redirect to the 'category-detail' URL pattern dynamically category_url = reverse('category-detail', args=[category_id]) return redirect(category_url)
Django Reverse URL Lookup with Namespaces:
# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_articles(request): # Redirect to the 'articles:article-list' URL pattern within the 'articles' namespace articles_url = reverse('articles:article-list') return redirect(articles_url)
Django URL Namespace Patterns:
# urls.py (project-level) from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls', namespace='articles')), ] # urls.py (app-level - articles) from django.urls import path app_name = 'articles' urlpatterns = [ path('list/', views.article_list, name='article-list'), ]
Django Reverse URL Resolution and Namespaces:
# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_admin(request): # Redirect to the 'admin:index' URL pattern within the 'admin' namespace admin_url = reverse('admin:index') return redirect(admin_url)
Django include()
and Namespace Conflicts:
include()
function to avoid naming clashes.# urls.py (project-level) from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls', namespace='articles')), path('products/', include('products.urls', namespace='articles')), # Namespace conflict ]
Django App-Level URL Namespaces:
# urls.py (app-level - myapp) from django.urls import path app_name = 'myapp' urlpatterns = [ path('list/', views.item_list, name='item-list'), ]
Django Reverse URL Patterns in Views:
reverse()
function within views to generate URLs dynamically based on view names.# views.py from django.urls import reverse from django.shortcuts import render def generate_dynamic_url(request): # Generate a dynamic URL using reverse() dynamic_url = reverse('some-view-name', args=[1]) return render(request, 'template.html', {'dynamic_url': dynamic_url})
Django Named URL Patterns with Namespaces:
# urls.py (app-level - myapp) from django.urls import path app_name = 'myapp' urlpatterns = [ path('list/', views.item_list, name='item-list'), ] # template.html <a href="{% url 'myapp:item-list' %}">Item List</a>