Django app_name namespace and namespace

Preface: django namespace solve the problem?

  1. Between multiple app, it is possible to produce url of the same name, to avoid reverse url when this time of confusion, you can use the application namespace do distinction. Application namespace to use. App_name defined in the application url

  2. A app, you can create multiple url mapped to within an app, so the question arises, when do the reverse, if you use the namespace will be confused, in order to avoid this problem. We can use the example of a namespace. Add namespace to include in the function.

The following two cases illustrate

One: Scene 1, different applications namespaces

(1) Application of the new app

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

(2) Main Configuration 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('app01/', include('app01.urls')),
    path('app02/', include('app02.urls')),
]

(2) app01 disposed 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 arranged 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 arranged 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 arranged 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 each app urls below defines a name = index and name = login. The reverse of each of the following views are login. This time django go back to looking for? When we open the browser path: http://127.0.0.1:8000/app01 , we will find a jump anomaly, even jump to the address: http://127.0.0.1:8000/app02/login above.

This time we use the namespace app_name, can be a good solution to this problem.

Change setting

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

from django.urls import path
from . import views

#命名空间
app_name = 'app01'   #这里变化了

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

(2) app01 disposed 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"))   #这里变化了

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

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

from django.urls import path
from . import views

app_name = 'app02'   #这里变化了

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

(4) app02 arranged 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"))  #这里变化了

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

When we open the browser path: http://127.0.0.1:8000/app01 and it will normally jump address: http://127.0.0.1:8000/app01/login the

II: Scene 2, the same application namespace

First start a new instance, #python manage.py startapp book

1. We look at the code that case, assuming that

(1)主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, point to the same app.

Son URL (2) application of the 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!!!!")

Through the above case, we can know.
When executed http://127.0.0.1:8000/book2/ will jump http://127.0.0.1:8000/book2/login
when it was found to perform http://127.0.0.1:8000/book1/ or jump http://127.0.0.1:8000/book2/login

This is not the result we want, we want to access / book1 / time jump / book1 / login; access / book2 / time jump / book2 / login; then pass two instances of an application, how do we distinguish?

2. Modify Case

(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')),  #变更部分
    path('book2/', include('book.urls'),  namespace='book2')),  #变更部分
]

Son URL (2) Application 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) View

def book_list(request):
        #获取当前namespace名称。
    current_namespace = request.resolver_match.namespace   #变更部分
    if request.GET.get("username"):
        return HttpResponse("My book list !!!!")
    else:
        print(current_namespace)
                #动态返回命名空间信息
        return redirect(reverse("%s:login"% current_namespace))  #变更部分

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

Guess you like

Origin blog.51cto.com/jiajinh/2432449