Django namespace app_name and namespace

Foreword: What does the django namespace solve?

  1. It is possible to have a url with the same name among multiple apps, so avoid confusion when reversing the url and use the application namespace to distinguish between them.Apply namespace usage.Define app_name in the application url

  2. An app can create multiple URLs that are mapped to one app, so there is a problem that can be avoided by using namespaces when doing the reverse.We can use the instance namespace.Add namespace to the include function.

Here are two examples

Scenario 1, Namespaces in different applications

(1) New app application

#python manage.py startapp app01
#python manage.py startapp app02

(2) Primary URL configuration

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app01/', include('app01.urls')),
    path('app02/', include('app02.urls')),
]

(2) app01 configuration project_django/app01/urls.py

from django.urls import path
from . import views

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

(3) app01 configuration project_django/app01/views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect,reverse

# Create your views here.

def index(request):
    if request.GET.get("username"):
        return HttpResponse("front page app01!")
    else:
        return redirect(reverse("login"))

def login(request):
    return HttpResponse("Login page app01!")

(4) app02 configuration project_django/app02/urls.py

from django.urls import path
from . import views

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

(5) App02 configuration project_django/app02/views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, reverse

# Create your views here.

def index(request):
    if request.GET.get("username"):
        return HttpResponse("front page app02!")
    else:
        return redirect(reverse("login"))

def login(request):
    return HttpResponse("Login page app02!")

We can see that the urls below each app define name=index and name=login.The reverse below each view is a login.Who will django go back to at this time?When our browser opens the path: http://127.0.0.1:8000/app01 When we do, we find a jump exception and jump to the address: http://127.0.0.1:8000/app02/login Above.

This is a good solution when we use the namespace app_name.

Modify Configuration

(1) app01 configuration project_django/app01/urls.py

from django.urls import path
from . import views

#Namespace
app_name = 'app01'   #This has changed

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

(2) app01 configuration project_django/app01/views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect,reverse

# Create your views here.

def index(request):
    if request.GET.get("username"):
        return HttpResponse("front page app01!")
    else:
        return redirect(reverse("app01:login"))   #This has changed

def login(request):
    return HttpResponse("Login page app01!")

(3) app02 configuration project_django/app02/urls.py

from django.urls import path
from . import views

app_name = 'app02'   #This has changed

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

(4) app02 configuration project_django/app02/views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, reverse

# Create your views here.

def index(request):
    if request.GET.get("username"):
        return HttpResponse("front page app02!")
    else:
        return redirect(reverse("app02:login"))  #This has changed

def login(request):
    return HttpResponse("Login page app02!")

When our browser opens the path: http://127.0.0.1:8000/app01 When you do, you will normally jump to the address: http://127.0.0.1:8000/app01/login Yes

Scenario 2, Namespace in the same application

Start a new instance, #python manage.py startapp book

1. Let's first look at the code case and assume

(1) Main URL, project_django/project_django/urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('book1/', include('book.urls')),
    path('book2/', include('book.urls')),
]

Multiple url s, pointing to the same app.

(2) Apply the sub URL of book, project_django/book/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.book_list),
    path('login', views.book_login, name="login"),
]

(3) View, project_django/book/views.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from django.shortcuts import redirect,reverse

def book_list(request):
    if request.GET.get("username"):
        return HttpResponse("My book list !!!!")
    else:
        return redirect(reverse("login"))

def book_login(request):
    return HttpResponse("Please Login!!!!")

From the above case, we can know.
When http://127.0.0.1:8000/book2/is executed, http://127.0.0.1:8000/book2/login is skipped
Discover or skip http://127.0.0.1:8000/book1/when executing http://127.0.0.1:8000/book2/login

This is not what we want, we want to skip/book1/login when we visit/book1/and/book2/login when we visit/book2/so how do we distinguish between two instances of an application?

2. Case Modification

(1) Main URL

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('book1/', include('book.urls'),  namespace='book1')),  #Change Part
    path('book2/', include('book.urls'),  namespace='book2')),  #Change Part
]

(2) Apply the sub URL of book

from django.urls import path
from . import views

app_name = "book"

urlpatterns = [
    path('', views.book_list),
    path('login', views.book_login, name="login"),
]

(3) Views

def book_list(request):
        #Gets the current namespace name.
    current_namespace = request.resolver_match.namespace   #Change Part
    if request.GET.get("username"):
        return HttpResponse("My book list !!!!")
    else:
        print(current_namespace)
                #Dynamic return of namespace information
        return redirect(reverse("%s:login"% current_namespace))  #Change Part

def book_login(request):
    return HttpResponse("Please Login!!!!")

Tags: Web Development Django Python

Posted on Sun, 25 Aug 2019 11:35:11 -0700 by yuan