Django 學習次日——URL路由及模板渲染方式

URL 的概念及格式:html

  URL的引入:客戶端:知道了url 就能夠去進行訪問;django

  服務端:設置好了url,別人才能訪問到我app

  URL :網址(全球統一資源定位符);由 協議,域名(ip port) ,路徑,參數,錨點等組成函數

 

 

django路由系統:post

  當一個請求到來時:ui

    一、首先到項目目錄下的urls.py(URLconf 根路徑配置模塊),查找路由規則;編碼

 

    二、根URLconf模塊,裏面定義了 urlpatterns 變量url

    三、urlpatterns 是一個(django.urls.path, django.urls.re_path 對象)列表spa

    四、按順序運行每一個 url 模式,在第一個匹配的模式中止3d

    五、一旦匹配,django 導入並調用給定的視圖(views.py 文件)

 

    六、若是中間出錯,或者沒有匹配到,返回404

  django 搜索 url 只搜索路徑部分,跟參數,與請求方法(get ,post)無關;

 

path 和 re_path:

  path基本規則:

 

 

    path(route, view, kwargs=None, name=None)

      route 是一個字符串的 url 規則

      view 是個視圖

      kwargs 額外參數,傳遞給 view ,必須是一個字典

      name url的命名

 

在 url 中捕獲參數:

  在 url 規則中使用 <變量名> 能夠捕獲 url 中的值傳遞給視圖 (捕獲的值是字符串)

 

路徑轉換:

轉換器的使用:

一、設置 url:

from django.urls import path 

urlpatterns = [
          path('test/<int:xx>',views.test),
        ]

 

二、在視圖中將獲取到的參數和參數的類型打印出來:

from django.http import HttpResponse 

def test3(request,xx):
  print(xx,type(xx))
  return HttpResponse('hello %s'%xx)

  注:同一個試圖的變量名 (xx)要一致

 

經常使用的轉換器:

  str 匹配除了路徑分隔符 / 以外的非空字符串,這是默認的形式

  int 匹配正整數,包含0

  slug 匹配字母,數字及橫槓,下劃線組成的字符串;任意ASCII碼

  uuid 匹配格式化的 uuid ,如 075194d3-6885-417e-a8a8-6c931e272f00

  path 匹配任何非空字符串,包含了路徑分隔符

re_path 正則匹配:

  格式:

from django.urls import re_path 

urlpatterns = [ re_path(r'test/(?P<變量名>正則),views.test), ]

 

包含其餘的 URLconfs :用 include

  一個project有一個總的urls.py各個app也能夠本身創建本身的urls.py用include()函數在project的urls.py文件進行註冊

  在根目錄的 url 配置文件(urls.py)中添加

 

 

傳遞額外參數:

path,re_path 方法中,傳遞一個kwargs 的字典參數給視圖函數;視圖函數能夠經過 key 獲取到

# 寫在主 URL 配置文件會給其路徑下的每個路由都加上該參數: 
from django.urls import path,include

urlpatterns = [
  path('test/',include('test1.urls'),kwargs={'key':'value'})
  path('test/',views.test,kwargs={'key':'value'})
  ]

 

 

name的做用:

  給一個匹配的 url 地址取名字;通常用於模板,也可使用 reverse 進行頁面重定向

 

 

 

跳轉路由:

# views.py 文件裏: 

from django.shortcuts import redirect,reverse def article(request,**kwargs) return redirect(reverse('new_artivle')) # 這裏能夠直接使用 剛剛自定義好的 name 值 return redirect('http://www.baidu.com') # 直接填入要跳轉的 url return 
   redirect('/test/test1/') # 直接填入自定義的路由

  redirect 是重定向,reverse是將 url 的 name 解析成 url 自己的函數

 

  app_name :

    定義在 app 文件夾下的 urlconf 模塊中

    app_name = app 名稱

# 當前app 中 urls.py 文件裏 
 app_name = 'test' urlpatterns = [ path('test/',views.test,name="xx"), ]

 

 

# 當前 app 中 views.py 文件裏 
from django.shortcuts import redirect,reverse def test(request): return redirect(reverse('test:xx'))

  這樣能夠避免兩個不一樣的 app 定義了同樣的 name

  name 參數能夠給這個 url 取一個合適的名字,經過給 url 取名字,之後再 view 或者模板中使用這個 url,就只須要使用這個名字就能夠了,這樣作的緣由是防止 url 的規則更改,會致使其餘地方用了這個 url 的地方都須要更改,可是若是取名字了,就不要作任何改動了

 

模板路徑配置:

  模板放在哪?

    一、再主目錄下建立一個 templates 目錄用來存放全部的 html 的模板文件;

    二、templates 目錄裏面再建立各個以 app 名字命名的目錄來存放各個app中的模板文件

 

setting.py 中的模板路徑配置:

將設置好的存放 html 模板的 templates 目錄路徑添加到 DIRS 中

'DIRS':[os.path.join(BASE_DIR,'templates')],

 

 

模板渲染方式:

  一、直接將 html 字符串硬編碼 HttpResponse 中

from django.http import HttpResponse def test(request): return HttpResponse('<h1 style="color:red">hello world</h1>')

 

  二、django.template.loader 定義了函數以加載模板

from django.template.loader import get_template from django.shortcuts import render from django.http import HttpResponse def test(request): t = get_template('test/test.html') # 這裏直接寫 templates 裏的路徑 
    html = t.render() return HttpResponse(html)

 

  三、使用 render 進行渲染:

from django.shortcuts import render def test(request): return render(request,'test/test.html') # 這裏直接寫 templates 裏的路徑

  render 方法是 django 封裝好用來渲染模板的方法很方便,很好用!

相關文章
相關標籤/搜索