1. 程式人生 > >Django urls 路由

Django urls 路由

  • 寫url和檢視的的對應關係
    from django.conf.urls import url
    from django.contrib import admin
    from app名 import views
    urlpatterns = [

    # FBV 寫法
    url(r'^admin/', admin.site.urls), # admin 後臺管理
    # 對應 檢視中的函式
    url(r'login/', views.login, name='login'),
    # 正則分組
    url(r'del_(student|teacher|class)/(\d+)/', views.delete, name='del'),
    # 正則命名分組
    url(r'^data/(?P

       # name='login'  url命名  用於 地址的反向解析

    # CBV 寫法
    # 在views.py檔案中 寫一個類 繼承 View 其中寫 get post 方法
    # 匯入方法from django.views import View
    url(r'index/', views.類名.as_view(), name='index'),

    ]

  • url路由分發
    from django.conf.urls import url, include
    from django.contrib import admin
    from crm import views

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    
        url(r'^show/', include('app名.urls檔名', namespace='show')),
        # namespace='show' 名稱空間 
    ]