1. 程式人生 > >Django2 URL配置

Django2 URL配置

分發 順序 auth pattern play gop fff namespace get

函數、類的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": "[email protected]"},
    "2": {"name": "root2", "email": "[email protected]"},
    "3": {"name": "root3", "email": "[email protected]"},
    "4": {"name": "root4", "email": "[email protected]"},
    "5": {"name": "root5", "email": "[email protected]"},
}

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": "[email protected]"},
    "2": {"name": "root2", "email": "[email protected]"},
    "3": {"name": "root3", "email": "[email protected]"},
    "4": {"name": "root4", "email": "[email protected]"},
    "5": {"name": "root5", "email": "[email protected]"},
}
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配置