django-URL實例命名空間(十一)

每生成一個地址,都是一個實例。使用實例命名空間,針對於一個app而言。django

book/views.pyapp

from django.http import HttpResponse from django.shortcuts import render,redirect,reverse from django.urls import resolve # Create your views here. def index(request): username = request.GET.get("username") if username is not None: return HttpResponse("welcome!") else: path=request.path current_namespace=resolve(path).namespace
        return redirect(reverse('{}:loose'.format(current_namespace),kwargs={'a':100,'b':200})) def error(request,a,b): sum=a+b return HttpResponse("<h1>path:{}</h1>".format(request.path))

book/urls.pyurl

from django.urls import path from . import views app_name ="book" urlpatterns = [ path('', views.index,name='index'), path('error/<int:a>/<int:b>', views.error,name='loose'), ]

主urls.pyspa

from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('book/',include('book.urls',namespace="book")), path('page/',include('book.urls',namespace="page")), ]

 

相關文章
相關標籤/搜索