URL是用戶請求路徑與views視圖處理函數的一個映射css
這裏是pycharm編輯開發爲例,新建的django項目,會在url.py下自動生成這樣一段代碼:html
1 from django.contrib import admin 2 from django.urls import path 3 4 urlpatterns = [ 5 path('admin/', admin.site.urls), 6 ]
啓動服務器,頁面下方看到以下顯示:python
訪問這個網址,若是沒有問題,會進入這個頁面,說明安裝和建立過程都沒有錯誤。正則表達式
而後,能夠在url.py文件下設置路由了數據庫
from django.contrib import admin from django.urls import path from app01 import views #先導入views urlpatterns = [ path('admin/', admin.site.urls), path('login/',views.login), ]
緊接着設計views視圖函數django
from django.shortcuts import render,HttpResponse # Create your views here. def login(request): if request.method =="POST": user = request.POST.get('user') pwd = request.POST.get('pwd') print(pwd) return HttpResponse(user) return render(request,'login.html')
視圖函數裏沒有對用戶名和密碼進行設計,具體到項目應該到數據庫提取數據,在這裏只簡單示範一下流程bootstrap
接下來就是須要設計模板了,以展現給用戶看服務器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <form action="" method="post"> {% csrf_token %} <div class="form-group"> <label for="user">用戶名</label> <input type="text" class="form-control" name="user"> </div> <div class="form-group"> <label for="pwd">密碼</label> <input type="password" class="form-control" name="pwd"> </div> <input type="submit" class="btn btn-success" value="登陸"> </form> </div> </div> </div> </body> </html>
頁面設計不作過多解釋,惟一一個須要點明的地方就是{%csrf_token%},這是django框架裏面的一種機制,提交form表單必須帶上,否則會出錯。app
這樣一套簡單的路由配置及實現過程就完成了!框架
下面迴歸到路由配置
簡單固定的路由配置使用path就能夠,可是一些不固定的複雜的路由若是每個狀況設置一個路由,就也須要再設置一個視圖函數,那就不合理了,這時候就須要使用正則表達式來解決這樣的問題。與之相匹配的是re_path()
例如:re_path(r'books/',views.books) 表示查看全部書籍
re_path(r'book/(\d+)/del',views.del) 表示刪除某一本書籍 其中()表示向視圖函數傳入一個數據,\d+ 表明的是匹配規則,匹配數字,以此表明某一本數的id,以位置參數傳入視圖函數
上邊的例子講的是簡單的路徑和位置傳參,在高級的配置中使用命名正則表達式組來捕獲正則表達式的值,而且以關鍵字參數傳參給視圖函數。
其語法是(?P<name>pattern)
,其中name
是組的名稱,pattern
是要匹配的模式。
from django.conf.urls import url from . import views urlpatterns = [ re_path(r'^articles/2003/$', views.special_case_2003), re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail), ]
捕獲的值將做爲關鍵字參數傳入視圖函數 /articles/2005/03/ 請求將調用views.month_archive(request, year='2005', month='03')函數,而不是views.month_archive(request, '2005', '03') /articles/2003/03/03/ 請求將調用函數views.article_detail(request, year='2003', month='03', day='03')
from django.urls import path,re_path,include from app01 import views urlpatterns = [ re_path(r'^admin/', admin.site.urls), re_path(r'^blog/', include('blog.urls')), ]
將路由指向blog包下的urls.py文件下的url,訪問網址 .../blog/blog下的url
有時候,咱們設計的url可能會在後續的開發維護中被更改,可能就會出現找不到等問題,爲了避免讓這個url無效或者過時,就能夠用反向解析。
在須要URL 的地方,對於不一樣層級,Django 提供不一樣的工具用於URL 反查:
django.core.urlresolvers.reverse()
函數。urls中添加一個name屬性,給url起名
from django.conf.urls import url from . import views urlpatterns = [ #... url(r'^articles/([0-9]{4})/$', views.year_archive, name='news-year-archive'), #... ]
模板中的實現:
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a> <ul> {% for yearvar in year_list %} <li><a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a></li> {% endfor %} </ul>
在python代碼中的實現:
from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect def redirect_to_year(request): # ... year = 2006 # ... return HttpResponseRedirect(reverse('news-year-archive', args=(year,))) # 同redirect("/path/")
命名空間(英語:Namespace)是表示標識符的可見範圍。一個標識符可在多個命名空間中定義,它在不一樣命名空間中的含義是互不相干的。這樣,在一個新的命名空間中可定義任何標識符,它們不會與任何已有的標識符發生衝突,由於已有的定義都處於其它命名空間中。
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^app01/', include("app01.urls",namespace="app01")), url(r'^app02/', include("app02.urls",namespace="app02")), ]
urlpatterns = [ url(r'^index/', index,name="index"), ]
urlpatterns = [ url(r'^index/', index,name="index"), ]
from django.core.urlresolvers import reverse def index(request): return HttpResponse(reverse("app01:index"))
from django.core.urlresolvers import reverse def index(request): return HttpResponse(reverse("app02:index"))