Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
# blog/urls.py — application namespace defined here from django.urls import path from . import views app_name = 'blog' urlpatterns = [ path('', views.PostListView.as_view(), name='post-list'), path('<int:pk>/', views.PostDetailView.as_view(), name='post-detail'), path('create/', views.PostCreateView.as_view(), name='post-create'), ] # accounts/urls.py from django.urls import path from . import views app_name = 'accounts' urlpatterns = [ path('register/', views.RegisterView.as_view(), name='register'), path('profile/', views.ProfileView.as_view(), name='profile'), ] # mysite/urls.py — root URLconf from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('blog/', include('blog.urls')), # app_name sets namespace path('accounts/', include('accounts.urls')), path('auth/', include('django.contrib.auth.urls')), ] # Reversing namespaced URLs in views from django.urls import reverse from django.shortcuts import redirect def my_view(request): return redirect(reverse('blog:post-list')) # Reversing in templates # {% url 'blog:post-detail' pk=post.pk %} # {% url 'accounts:register' %}
Result
Open