• 【Django】url(路由系统)


    1、单一路由对应

      url(r'^index/',views.index),

    2.基于正则的路由

      url(r'^index/(d*)', views.index),

      url(r'^manage/(?P<name>w*)/(?P<id>d*)', views.manage),
    3.默认值
    urlpatterns = [
    
        url(r'^index/',views.index,{'name':'root'}),
    
    ]
    project/urls.py
    from django.shortcuts import render,HttpResponse,redirect
    from django.urls import reverse
    
    # Create your views here.
    def index(request,name):
        print(name)
        return HttpResponse(name)
    app01/views.py

    4.路由分发

    from django.contrib import admin
    from django.conf.urls import url,include
    
    urlpatterns = [
        url('^admin/', admin.site.urls),
        url(r'^app01/',include("app01.urls")),
        url(r'^app02/',include("app02.urls")),
    ]
    project/urls.py
    from django.contrib import admin
    from django.conf.urls import url
    from app01 import views
    
    urlpatterns = [
        url('^index/', views.index),
        url('^user_info/', views.user_info),
        url('^userdetail-(?P<nid>d+)/', views.user_detail),
        url('^home/', views.Home.as_view()),
    ]
    app01/urls.py
    from django.contrib import admin
    from app02 import views
    from django.conf.urls import url
    
    urlpatterns = [
        url('^login/', views.login),
    ]
    app02/urls.py
    5.命名空间
    from django.contrib import admin
    from django.conf.urls import url,include
    from app01 import views
    
    urlpatterns = [
        url(r'^app01/', include('app01.urls', namespace='author')),
    ]
    project/urls.py
    from django.contrib import admin
    from django.conf.urls import url,include
    from app01 import views
    
    app_name = 'app01'
    
    urlpatterns = [
        url(r'^index/',views.index,name='daly'),
    ]
    app01/urls.py
    from django.shortcuts import render,HttpResponse,redirect
    from django.urls import reverse
    
    # Create your views here.
    def index(request):
        v = reverse('author:daly')
        print(v)
        return HttpResponse('OK')
    app01/views.py

  • 相关阅读:
    使用PrintDocument进行打印
    【转】封装原生JS实现Ajax
    休眠到指定时分秒
    [原创]vscode初体验
    反编译网站
    命令行工具aspnet_regiis.exe实现加密和解密web.config
    Sqlserver内置函数实现MD5
    [转]如何循序渐进向dotnet架构师发展
    [转]高级系统架构师培训笔记
    理解RESTful
  • 原文地址:https://www.cnblogs.com/dalyday/p/8971232.html
Copyright © 2020-2023  润新知