首页 > 代码库 > Django2 URL配置

Django2 URL配置

函数、类的URL定义

"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r‘^$‘, views.home, name=‘home‘)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r‘^$‘, Home.as_view(), name=‘home‘)
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r‘^blog/‘, include(‘blog.urls‘))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r^admin/, admin.site.urls),
    # 视图为函数,定义URL
    url(r^index/, views.index),
    # 视图为类,定义URL
    url(r^home/, views.Home.as_view()),
]

 


路由匹配

URL有很多种类型,一个URL可以对应一个视图或者类,也可以用正则表达式的方法定义URL去匹配一类的URL

一对一的URL

from django.conf.urls import url
from django.contrib import admin
 from app01 import views
urlpatterns = [
    url(r^admin/, admin.site.urls),
    # 视图为函数,定义URL
    url(r^index/, views.index),
    # 视图为类,定义URL
    url(r^home/, views.Home.as_view()),
]

一对多的URL

通过GET方法传参

http://127.0.0.1:8000/detail/?nid=1

http://127.0.0.1:8000/detail/?nid=2

http://127.0.0.1:8000/detail/?nid=3

技术分享
url(rdetail, views.detail),
urls.py
技术分享
USER_DICT = {
    "1": {"name": "root1", "email": "root1@live.com"},
    "2": {"name": "root2", "email": "root2@live.com"},
    "3": {"name": "root3", "email": "root3@live.com"},
    "4": {"name": "root4", "email": "root4@live.com"},
    "5": {"name": "root5", "email": "root5@live.com"},
}

def index(request):
    return render(request, "index.html", {"user_dict": USER_DICT})
     
def detail(request):
    nid = request.GET.get("nid")
    detail_info = USER_DICT[nid]
    return render(request, "detail.html", {"detail_info": detail_info})
views.py
技术分享
<ul>
{% for k, row in user_dict.items %}
 <li><a target="_blank" href="/detail/?nid={{ k }}">{{ row.name }}</a></li>
{% endfor %}
</ul>
index.html
技术分享
<h1>详细信息</h1>
<h6>用户名:{{ detail_info.name }}</h6>
<h6>邮箱:{{ detail_info.email }}</h6>
detail.html

通过正则方法,动态传参,这种定义方式形参和实参的位置要一致

http://127.0.0.1:8000/detail-1.html

http://127.0.0.1:8000/detail-2.html

http://127.0.0.1:8000/detail-3.html

技术分享
url(r^detail-(\d+).html, views.detail),
urls.py
技术分享
USER_DICT = {
    "1": {"name": "root1", "email": "root1@live.com"},
    "2": {"name": "root2", "email": "root2@live.com"},
    "3": {"name": "root3", "email": "root3@live.com"},
    "4": {"name": "root4", "email": "root4@live.com"},
    "5": {"name": "root5", "email": "root5@live.com"},
}
def index(request):
    return render(request, "index.html", {"user_dict": USER_DICT})
def detail(request,nid):
    detail_info = USER_DICT[nid]
    return render(request, "detail.html", {"detail_info": detail_info})
views.py
技术分享
<ul>
    {% for k, row in user_dict.items %}
        <li><a target="_blank" href="/detail-{{ k }}.html">{{ row.name }}</a></li>
    {% endfor %}
</ul>
index.html
技术分享
<h1>详细信息</h1>
<h6>用户名:{{ detail_info.name }}</h6>
<h6>邮箱:{{ detail_info.email }}</h6>
detail.html

通过正则方法,动态传参,这种方式相当于关键字参数,形参和实参的位置可以不一致

技术分享
url(r^detail-(?P<nid>\d+)-(?P<uid>\d+).html, views.detail),
urls.py

多参数可以写成

def detail(request, *args, **kwargs):
    pass
     
# 以第一种方式传递的参数,都会传递到*args中
url(r^detail-(\d+)-(\d+).html, views.detail),

# 以第二种方式传递的参数,都会传递到**kwargs中
url(r^detail-(?P<nid>\d+)-(?P<uid>\d+).html, views.detail),

 


URL命名

URL路由对应的名称 name=xxx,对URL路由关系进行命名,可以根据此名称生成自己想要的URL

通过URL对应的名称,action会自动匹配URL,修改URL的时候就不用每次修改完还要再改HTML中的URL了

url(r^index/, views.index, name="index"),
<form action="{% url "index" %}" method="POST">
        <p><input type="text", name="user", placeholder="用户名"/></p>
        <p><input type="text", name="email", placeholder="邮箱"/></p>
        <input type="submit" value="提交">
</form>

 


URL命名空间

mysite下的urls.py, namespace设置命名空间

技术分享
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from app01 import views
 
urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^a/, include(app01.urls, namespace="author")),
    url(r^b/, include(app01.urls, namespace="publisher")),
]
urls.py

app01下的urls.py

技术分享
from django.conf.urls import url
from django.contrib import admin
from app01 import views
 
app_name = "app01"
 
urlpatterns = [
    url(r^index/, views.index, name=index),
]
urls.py

views.py 在视图中使用, reverse反转时"namespace:name"

技术分享
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
from django.urls import reverse
 
 
def index(request):
    v = reverse("author:index")
    print(v)
    return HttpResponse("OK")
views.py

模板中使用

技术分享
{% url namesapce: name%}
index.html

 


URL和模板的对应关系

技术分享
url(r^test/, views.index, name="test"),
url(r^test/(\d+)/(\d+)/, views.index, name="test"),
url(r^test/(?P<nid>\d+)/(?P<uid>\d+)/, views.index, name="test"),
urls.py
技术分享
{% url "test" %}
{% url "test" 1 2 %}    # url为/test/1/2/    参数要按照顺序
{% url "test" uid=9 nid=4 %} # url为/test/4/9/    参数可以不按照顺序
test.html

 


URL和视图的对应关系

技术分享
url(r^test/, views.index, name="test"),
url(r^test/(\d+)/(\d+)/, views.index, name="test"),
url(r^test/(?P<nid>\d+)/(?P<uid>\d+)/, views.index, name="test"),
urls.py
技术分享
from django.urls import reverse 
 
def test(request, *args, **kwargs):
    v = reverse("index")
    return render(request, "test.html", {"url": v})
     
def test(request, *args, **kwargs):
    v = reverse("index", args=(1,2,))
 
    return render(request, "test.html", {"url": v})    
     
def test(request, *args, **kwargs):
    v = reverse("index", kwargs={"nid": "3", "uid": "4"})
    return render(request, "test.html", {"url": v})
views.py
技术分享
<form action="{{ url }}" method="POST">
    <p><input type="text", name="user", placeholder="用户名"/></p>
    <p><input type="text", name="email", placeholder="邮箱"/></p>
    <input type="submit" value="提交">
</form>
test.html

 


URL设置默认值

技术分享
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": "root"}),
]
urls.py
技术分享
from django.shortcuts import render
from django.shortcuts import HttpResponse
# Create your views here.
from django.urls import reverse
 
 
def index(request, name):
    print(name)
    return HttpResponse("OK")
views.py

 


路由分发

通过工程下面的urls.py去分发路由,将不同后缀的URL分发给不同的app

技术分享
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include

urlpatterns = [
    url(r^cmdb/, include("app01.urls")),
    url(r^monitor/, include("app02.urls")),
]
工程下面的urls.py
技术分享
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r^login/, views.login),
]
app01 urls.py
技术分享
from django.conf.urls import url
from django.contrib import admin
from app02 import views

urlpatterns = [
    url(r^login/, views.login),
]
app02 urls.py

Django2 URL配置