在 Django
项⽬中,⼀个常⻅需求是获取最终形式的 URL
,⽐如⽤于嵌⼊⽣成的内容中(视图和资源⽹址,给⽤户展示⽹址等)或⽤户服务器端的导航处理(重定向等)。
强烈建议不要硬编码 URL
(这是⼀个费⼒、不能扩展、容易出错的主意)。同样危险的是设计临时机制来⽣成的 URL
与URLconf
描述的设计的URL
⼀样,这会导致URL
随着时间的推移变得过时。
换句话说,需要的是 DRY
机制。除其他优势外,它还允许 URL
设计⾃动更新,⽽不必遍历所有项⽬代码来搜索和替换过时的 URL
。
Django
提供执⾏反转 URL
的⼯具,这些⼯具与需要 URL
的不同层匹配:
url
模板标签。Python
编码:使⽤ reverse()
函数。在定义路由的时候,可以为路由命名,⽅便查找特定视图的具体路径信息。
1.在定义普通路由时,可以使⽤name
参数指明路由的名字,如:
# -*- coding: utf-8 -*-
# @File : urls.py
# @author: 北极的三哈
# @email : Flymeawei@163.com
# @Time : 2022/10/29 上午5:34
""""""
from django.urls import path, re_path
from project01 import views
urlpatterns = [
path('login/', views.loginView),
path('other/sanha/awei/', views.otherView, name='other'),
]
2.在使⽤include
函数定义路由时,可以使⽤namespace
参数定义路由的命名空间。
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('mydj/', include(('project01.urls', 'project01'), namespace='project01-app'))
]
命名空间表示,凡是film.urls
中定义的路由,均属于namespace
指明的filmapp
名下。
命名空间的作⽤:避免不同应⽤中的路由使⽤了相同的名字发⽣冲突,使⽤命名空间区别开。
使⽤reverse
函数,可以根据路由名称,返回具体的路径。
根路由
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('mydj/', include('project01.urls'))
]
子路由
# -*- coding: utf-8 -*-
# @File : urls.py
# @author: 北极的三哈
# @email : Flymeawei@163.com
# @Time : 2022/10/29 上午5:34
""""""
from django.urls import path, re_path
from project01 import views
urlpatterns = [
path('login/', views.loginView),
path('other/sanha/awei/', views.otherView, name='other'),
]
views.py
from django.shortcuts import render
from django.http import HttpRequest, HttpResponseRedirect, HttpResponse
from django.urls import reverse
# Create your views here.
def loginView(request):
"""
:param request:
:return:
"""
url = reverse('other')
print(url)
return HttpResponseRedirect(url)
def otherView(request):
"""
:param request:
:return:
"""
return HttpResponse('OtherView')
访问:http://127.0.0.1:8000/mydj/login/
跳转:http://127.0.0.1:8000/mydj/other/sanha/awei/
根路由urls.py
子路由urls.py
# -*- coding: utf-8 -*-
# @File : urls.py
# @author: 北极的三哈
# @email : Flymeawei@163.com
# @Time : 2022/10/29 上午5:34
""""""
from django.urls import path, re_path
from project01 import views
urlpatterns = [
path('login/', views.loginView),
path('other/sanha/awei/' , views.otherView, name='other'),
]
视图函数views.py
from django.shortcuts import render
from django.http import HttpRequest, HttpResponseRedirect, HttpResponse
from django.urls import reverse
# Create your views here.
def loginView(request):
"""
:param request:
:return:
"""
# url = reverse('other')
# print(url)
# return HttpResponseRedirect(url)
return render(request, 'project01/index.html')
def otherView(request, uname):
"""
:param request:
:return:
"""
return HttpResponse('OtherView--%s' % uname)
templates/project01/index.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<a href="{% url 'other' 'Hello' %}">URLa>
body>
html>
访问:127.0.0.1:8000/mydj/login/
点击URL
重定向:http://127.0.0.1:8000/mydj/other/sanha/awei/Hello
# -*- coding: utf-8 -*-
# @File : urls.py
# @author: 北极的三哈
# @email : Flymeawei@163.com
# @Time : 2022/10/29 上午5:34
""""""
from django.urls import path, re_path
from project01 import views
urlpatterns = [
path('login/', views.loginView),
path('other/sanha/flyme/' , views.otherView, name='other'),
]
访问:127.0.0.1:8000/mydj/login/
重定向:http://127.0.0.1:8000/mydj/other/sanha/flyme/awei
对于未指明namespace
的,reverse
(路由name
)
对于指明namespace
的,reverse
(命名空间namespace:路由name
)
根路由:urls.py
视图函数:views.py
from django.shortcuts import render
from django.http import HttpRequest, HttpResponseRedirect, HttpResponse
from django.urls import reverse
# Create your views here.
def loginView(request):
"""
:param request:
:return:
"""
url = reverse('project01-app:other', args=['awei'])
print(url)
return HttpResponseRedirect(url)
# return render(request, 'project01/index.html')
def otherView(request, uname):
"""
:param request:
:return:
"""
return HttpResponse('OtherView--%s' % uname)
视图函数
index.html
页面
根路由
子路由
python manage.py runserver
访问:127.0.0.1:8000/mydj/login/