关于django的url的namespace和view写法
2021-05-29 08:03
标签:结果 admin div redirect def code direct 空间名 其他 即使不同的APP使用相同的URL名称,URL的命名空间模式也可以让你唯一反转命名的URL。 举个例子: 项目的urls.py写法: app01下的urls.py写法 app02下的urls.py的写法 app01下的views.py的写法 app02下的views.py的写法 你会发现,不管你是访问app01下的index还是app02下的index,打印的结果都是/app02/index/,也就是打印的是最后一个index别名对应的url路径。所以别名冲突了的话就需要我们的命名空间来保证别名对应的url的唯一性了。 project中的urls.py app01中的urls.py app02中的urls.py 现在,我的两个app中 url名称重复了,我反转URL的时候就可以通过命名空间的名称得到我当前的URL。 语法: ‘命名空间名称:URL名称‘ 模板中使用: views中的函数中使用 这样即使app中URL的命名相同,我也可以反转得到正确的URL了。 第二种写法:就是在每个app下的urls.py文件中指定app名称,同样是命名空间。 app01下的urls.py app02下的urls.py app01下的view视图中反向解析 app02下的view视图中反向解析 第三种写法: 项目的urls.py中的写法: 那么其他的app中的urls.py中就不需要写app_name=‘应用名‘了,看app01下urls.py的写法: app02下urls.py的写法: 视图的写法: 关于django的url的namespace和view写法 标签:结果 admin div redirect def code direct 空间名 其他 原文地址:https://www.cnblogs.com/zzj666/p/14747279.html命名空间模式
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^app01/‘, include(‘app01.urls‘)),
url(r‘^app02/‘, include(‘app02.urls‘)),
]
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
from django.conf.urls import url
from django.contrib import admin
from app02 import views
# app_name = ‘app02‘
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
from django.shortcuts import render,HttpResponse,redirect
from django.urls import reverse
# Create your views here.
def index(request):
print(reverse(‘index‘))
return HttpResponse(‘ok‘)
from django.shortcuts import render,HttpResponse,redirect
from django.urls import reverse
# Create your views here.
def index(request):
print(reverse(‘index‘))
return HttpResponse(‘ok2‘)
from django.conf.urls import url, include
urlpatterns = [
url(r‘^app01/‘, include(‘app01.urls‘, namespace=‘app01‘)),
url(r‘^app02/‘, include(‘app02.urls‘, namespace=‘app02‘)),
]
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r‘^(?P
from django.conf.urls import url
from app02 import views
urlpatterns = [
url(r‘^(?P
{% url ‘app01:detail‘ pk=12 pp=99 %}
v = reverse(‘app01:detail‘, kwargs={‘pk‘:11})
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
# url(r‘^app01/‘, include(‘app01.urls‘,namespace=‘app01‘)),
url(r‘^app01/‘, include(‘app01.urls‘)),
# url(r‘^app02/‘, include(‘app02.urls‘,namespace=‘app02‘)),
url(r‘^app02/‘, include(‘app02.urls‘)),
]
from django.conf.urls import url
from django.contrib import admin
from app01 import views
app_name = ‘app01‘
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
from django.conf.urls import url
from django.contrib import admin
from app02 import views
app_name = ‘app02‘
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
def index(request):
print(reverse(‘app01:index‘))
return HttpResponse(‘ok‘)
def index(request):
print(reverse(‘app02:index‘))
return HttpResponse(‘ok2‘)
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
# url(r‘^app01/‘, include(‘app01.urls‘,namespace=‘app01‘)),
url(r‘^app01/‘, include((‘app01.urls‘,‘app01‘),namespace=‘app01‘)),
# url(r‘^app02/‘, include(‘app02.urls‘,namespace=‘app02‘)),
url(r‘^app02/‘, include((‘app02.urls‘,‘app02‘),namespace=‘app02‘)),
]
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
from django.conf.urls import url
from django.contrib import admin
from app02 import views
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index,name=‘index‘),
]
#app01
def index(request):
print(reverse(‘app01:index‘))
return HttpResponse(‘ok‘)
#app02
def index(request):
print(reverse(‘app02:index‘))
return HttpResponse(‘ok2‘)
下一篇:http请求中的三种参数类型
文章标题:关于django的url的namespace和view写法
文章链接:http://soscw.com/essay/89029.html