标签:图片 abd rect view img 耦合 动态获取 rom space
根据名字动态获取到对应的路径
可以给每一个路由与视图函数对应关系起一个名字
这个名字能够唯一标识出对应的路径
注意这个名字不能重复是唯一的
通过别名来传递路径给到前端或者后端,好处是更加灵活,因为是通过别名进行传递,所以只要修改源路径,其他的层都不需要更改,否则如果直接通过源路径传递,则耦合度较高,不够灵活,维护成本太高
url(r'^pre/', views.press_list, name='press_list')
reverse(‘你给路由与视图函数对应关系起的别名‘)
from django.urls import reverse
reverse('press_list') ---> '/pre/'
{% url ‘你给路由与视图函数对应关系起的别名‘ %}
{% url 'press_list' %}
>>>> urls.py
url(r'^app01/', include('app01.urls'))
>>>> app01.urls.py
url(r'^index/(\d+)/(\d+)/$', views.index, name='index')
>>>> app01.views.py
from django.shortcuts import render, redirect, HttpResponse
def index(request, year, month):
# return HttpResponse(reverse('index', args=(year, month)))
return render(request, 'home.html', locals())
`
from django.url import reverse
reverse('index', args=(year, month)) ---> /app01/index/2019/10/
{% url ‘你给路由与视图函数对应关系起的别名‘ %}
{% url 'index' 2019 10 %}
>>>> urls.py
url(r'^app01/', include('app01.urls'))
>>>> app01.urls.py
url(r'^home/(?P<year>\d+)/(?P<month>\d+)/$', views.home, name='home')
>>>> app01.views.py
from django.shortcuts import render, redirect, HttpResponse
def home(request, year, month):
# return HttpResponse(reverse('home', kwargs={'year': year, 'month': month}))
return render(request, 'home.html', locals())
from django.url import reverse
reverse('home', kwargs={'year': year, 'month': month}) ---> /app01/home/2019/10
{% url 'home' month=2019 year=10 %}
针对有名分组与无名分组的反向解析统一采用一种格式即可
反向解析的本质:就是获取到一个能够访问名字所对应的视图函数
即使不同的APP使用相同的URL名称,URL的命名空间模式也可以让你唯一反转命名的URL。
不使用名称空间,两个app出现同名的路由名字,则匹配到就会匹配,这样的话两个app访问的是同一个网页,并不是不同的
先创建一个新的app
manage.py@dj4 > startapp app02
注册app
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
'app02.apps.App02Config',
]
urls.py
from django.conf.urls import url, include
urlpatterns = [
url(r'app01/',include('app01.urls')),
url(r'app02/',include('app02.urls')),
]
app01.urls
from django.conf.urls import url
from app01 import views
urlpatterns = [
url('^index/',views.index)
]
app01.views
def index(request):
return HttpResponse('index1')
app02.urls`
from django.conf.urls import url
from app01 import views
urlpatterns = [
url('^index/',views.index)
]
app02.views
def index(request):
return HttpResponse('index2')
测试
输入 http://127.0.0.1:8000/app01/index/
输入 http://127.0.0.1:8000/app02/index/
两个url访问的都是app01下的index的内容,这种是有问题的,如果需要每个app访问的都是自己的网页内容,但是路由名字又和其他app里的是一样的,这种用名称空间解决
给两个app的URL路由关系创建一个名称空间
urls.py
from django.conf.urls import url, include
urlpatterns = [
# 第一种写法
# url(r'app01/', include("app01.urls", namespace="app01")),
# url(r'app02/', include("app02.urls", namespace="app02")),
# 第二种写法
url(r'app01/', include(("app01.urls", "app01"))),
url(r'app02/', include(("app02.urls", "app02"))),
]
app01.urls.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
url('^index/', views.index)
]
app02.urls.py
from django.conf.urls import url
from app02 import views
urlpatterns = [
url('^index/',views.index)
]
app01.views.py
def index(request):
return HttpResponse('index1')
app02.views.py
def index(request):
return HttpResponse('index2')
标签:图片 abd rect view img 耦合 动态获取 rom space
原文地址:https://www.cnblogs.com/cjwnb/p/11790092.html