Django의 URL 루트 시스템 (관계 매핑,name 값, 루트 배달, 기본값, 이름 공간 namespace) [인터랙티브 편1]

12137 단어 Django
전편: Django의 창설 프로젝트[2]클릭 디렉터리 편: Django의 전후단 상호작용 편 디렉터리 클릭 다음 편: Django의view 보기 처리[상호작용 편2]클릭 점프
카탈로그
하나.URL 라우팅 매핑 관계식둘.url 루트의name값 셋.URL 라우팅 배포넷.기본값오.네임스페이스namespace하나.URL 라우팅 매핑 관계
실전 Django의 URL 라우팅 매핑 관계:https://blog.csdn.net/Burgess_zheng/article/details/86528727
1. (정적 URL) 하나의 URL은 하나의 실행 함수나 실행 클래스에 대응한다.
FBV:  

path('burgess_web/', views.func),#     Django    path

url(r'^burgess_web/', views.func), #2.0      url

#   url        views  func  

CBV: 


path('burgess_web/', views.Class.as_view()), #     Django    path

url(r'^burgess_web/', views.Class.as_view()), #2.0      url

#   url    views      Class(    djangon  )

2.( url) url

FBV: 

  # url    


#1.    
    re_path('burgess01-(\d+)-(\d+).html/', views.test01),  #     Django    path
    #url(r'^burgess01-(\d+)-(\d+).html/', views.test01),     #2.0      url
#2.    
    re_path('burgess02-(?P\d+)-(?P\d+).html/', views.test02),   #     Django    path
    #url(r'^burgess02-(?P\d+)-(?P\d+).html/', views.test02),      #2.0      url
#          
    re_path('burgess03-(?P\d+)-(?P\d+).html/', views.test03),    #     Django    path
    # url(r'^burgess03-(?P\d+)-(?

  # url 3


def test01(request,*args):
    print(args)
    return HttpResponse('args:%s'%json.dumps(args))

def test02(request,*args,**kwargs):
    print(kwargs)
    return HttpResponse('kwargs:%s'%json.dumps(kwargs))

def test03(request,nid,uid):
    print(nid,uid)
    return HttpResponse('nid:%s,uid:%s' %(uid,nid))

CBV: FBV

.url name

Django URL name :https://blog.csdn.net/Burgess_zheng/article/details/86529185

name ? url

1.html( ) name url ( )

    1.{% url 'indexx' 2 6 %}
        URL:  

             re_path('burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01'),# Django path
             #url(r'^burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01'),   #2.0 url
       Templates:

            {% url 'burgess01' 2 6 %}    # url
            burgess01 == burgess01-(\d+)-(\d+).html  # url
            'burgess01' 2 6 == burgess01-2-6.html
           # url:burgess01-13-16.html
           # url render
           #render html {% url 'burgess01' 2 6 %} == burgess01-2-16.html

    
    2.{{ request.path_info }}    
        URL:  

            re_path('burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01'),  # Django path
            #url(r'^burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01'),      #2.0 url

        Templates:

            {{ request.path_info }}      # url
            request.path_info ==burgess01-(\d+)-(\d+).html  # url
            # url:burgess01-1-2.html
            # url render
            #render html { request.path_info }} == burgess01-1-2.html

2.func name url ( )

    1.
        URL:  
            re_path('burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01'),  # Django path
            #url(r'^burgess01-(\d+)-(\d+).html/', views.test01,name='burgess01')      #2.0 url
     
       FUNC: 
            def test01(request,*args):
                from django.urls import reverse       # url
                names = reverse('burgess01', args={90,88,}) # url
                print(names)
                return render(request,'burgess.html',{'names':names})  # , html

    2.
        URL: 
            re_path('burgess02-(?P\d+)-(?P\d+).html/', views.test02,name='burgess02'),  
            #url(r'^burgess02-(?P\d+)-(?P\d+).html/', views.test02,name='burgess02'), 
        FUNC: 
            def index(request,nid,uid):
                from django.urls import reverse
                names = reverse('burgess02',kwargs={"nid":80,"uid":90})
                print(names)
                return render(request,'burgess.html',{'names':names})  # , html

.URL

Django URL :https://blog.csdn.net/Burgess_zheng/article/details/86529768

app , app urls.py , 。
,Monitor( ) , cmdb , 2 urls.py ., ( )
, ,Monitor ,openstyle..

project(urls.py):


from django.contrib import admin
from django.urls import path,re_path    #Django  2.0  
from django.conf.urls import url            #Django2.0
from django.conf.urls import include


urlpatterns = [
    path('admin/', admin.site.urls),

    #    
        #cmdb_app
    re_path('cmdb/',include("cmdb.urls")),
    #url(r'^cmdb/', include("cmdb.urls")),
        #monitor_app
    re_path('monitor/', include("monitor.urls")),
    # url(r'^monitor/', include("monitor.urls")),
]

app(urls.py):


from django.urls import path,re_path #Django  2.0  
from app_name import views
from django.conf.urls import url          #Django2.0
from django.conf.urls import include

urlpatterns = [
    re_path('',views.burgess_func),
    #url(r'',views.burgess_func),
]

.

url , ,url

#URL


from django.contrib import admin
from django.urls import path,re_path #Django  2.0  
from project_burgess import views
from django.conf.urls import url  #Django2.0

urlpatterns = [
    path('admin/', admin.site.urls)
    re_path('xxx/', views.func,{'name':'root'}),   #{'name':'root'}:   ==    
    #url(r'^xxx/', views.func,{'name':'root'}),    #{'name':'root'}:   ==    

]

#URL


from django.shortcuts import render,HttpResponse,redirect
def func(requst,name):  #name  :    url    
    return  HttpResponse('name')

 

. namespace

Django URL :https://blog.csdn.net/Burgess_zheng/article/details/86530913

URL URL

# URL


from django.contrib import admin
from django.urls import path,re_path #Django  2.0  
from project_burgess import views
from django.conf.urls import url  #Django2.0
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls)
    #    namespace
    re_path('a/',include("namespace.urls", namespace='author')),
    #url(r'^a/', include("namespace.urls", namespace='author')),
    re_path('b/',include('namespace.urls', namespace='publisher')),
    #url(r'^b/', include('namespace.urls', namespace='publisher')),

]

# URL(app )


__author__ = "Burgess Zheng"
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.urls import path,re_path #Django  2.0  
from namespace import views
from django.conf.urls import url  #Django2.0

app_name='namespace'
urlpatterns = [
    re_path('index/',views.index,name='index'),
   #url(r'^index/', views.index,name='index'),

]

#URL +name URL


from django.shortcuts import render,HttpResponse,redirect

# Create your views here.
def index(request):
    from django.urls import reverse  #   URL name      url
    a = reverse('author:index')  #     url          url
    print(a)#     127..../a/index/
    b = reverse('publisher:index') #     url          url
    print(b)#     127..../b/index/
    return HttpResponse('a:%s b:%s'%(a,b))

:Django 【 】
:Django  
:Django view 【 】 ​​​​​​​

 

좋은 웹페이지 즐겨찾기