• <menu id="sssag"></menu>
  • <menu id="sssag"></menu>
  • 上周內容回顧

    • 什么是ORM
    # 1.對象映射關系
    	類	》表
    	對象	》記錄
    	屬性	》字段值
        
    # 2.如何來使用
    class User(models.Model):
        # id int primary key auto_increment
        id = models.AutoField(primary_key=True)  # 如果表的主鍵名就是ID 則可以省略
        
        username = models.CharField(max_length=32, default=0, null-True)  # not null
        
    
    # 3.數據的增刪改查
        # 增
        # request.POST  =>  {'A':1, 'B':2, 'C':3}
        models.User.objects.create(username='ly', password=123)
        models.User.objects.create(**A)
        
        obj = models.User(username=123)
        obj.save()
        
        # 改
        models.User.objects.filter().update(username=123)
        
        obj = models.User.objects.filter(pk=1).first()  # 對象,不是queryset
        
        obj.username = 'aaa'
        obj.password = 'bbb'
        obj.save()
        
        # 刪
        filter().delete()
        
        # 查
        # filter里面的條件是and關系
        obj = models.User.objects.filter()
        obj = models.User.objects.filter().filter().all()  # 返回的結果是queryset對象
        """因為 返回結果是queryset 就可以繼續使用queryset提供的點方法"""
        
        
    # 4.ORM創建外鍵關系
        01 一對一關系 外鍵字段建在使用頻率高的一方
        models.OneToOneField(to='AuthorDetail', to_field='')
        
        02 一對多 外鍵字段建在多的一方
        models.ForeignKey(to='')
        
        03 多對多 外鍵建在第三張表 不需要我們手動創建
        authors = models.ManyToManyField(to='')
    

    今日內容概要

    • Django 的請求生命周期流程圖
    • 路由層
      • 無名有名分組
      • 反向代理
    • 路由分發
    • 名稱空間
    • 虛擬環境演示

    內容詳細

    1. Django的請求生周期流程圖

    # 幫助我們梳理django框架的執行順序
    

    image

    2. 路由層

    # 路由匹配 urls.py中
    	# 在Django1.x中 創建新的項目后 先在配置文件settings中修改配置 再去啟動項目:
    """
    TEMPLATES = [
        {
    'DIRS': [os.path.join(BASE_DIR, 'templates')]
    """
                
        # url屬性中是正則匹配 匹配規則:從上到下只要正則表達式匹配成功,就不會往下匹配了
        # 在urls.py文件中寫入:
    """
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.test),  # 客戶輸入默認域名 就是訪問首頁
        url(r'^test/$', views.test),
        url(r'^testadd/$', views.testadd),
    ]
    """
    
        # 在app目錄下的views.py文件中寫入:
    """
    from django.shortcuts import render, HttpResponse
    # Create your views here.
    
    def test(request):
        print('test')
        return HttpResponse('test')
    
    def testadd(request):
        print('testadd')
        return HttpResponse('testadd')
    """
    
    
    	# 補充
    		對于Django自動補全/功能 如果不想要 也可以在settings.py文件中增加配置項:
    			APPEND_SLASH = False  # 控制django是否自動加斜杠匹配
                
    		get請求的傳參方式:
    			http://127.0.0.1:8000/test/?a=1
    			http://127.0.0.1:8000/test/1
    

    image

    image

    3. 無名分組和有名分組

    # 無名分組
    	# 在urls.py文件中寫入:
    		# 無名分組
    		url(r'^test/(\d+)', views.test),  # 會把括號中匹配的數字當成 位置參數傳給視圖函數
            
    	# 在app目錄下的views.py文件中寫入:
    """
    from django.shortcuts import render, HttpResponse
    # Create your views here.
    
    def test(request, aaa):
        print(aaa)
        print('test')
        return HttpResponse('test')
    """
            
    
    # 有名分組
    	# 在urls.py文件中寫入:
    		# 有名分組
    		url(r'^test/(?P<bbb>\d+)$', views.test),  # 會把括號中匹配的數字當成 關鍵字參數傳給視圖函數
    
    	# 在app目錄下的views.py文件中寫入:
    """
    from django.shortcuts import render, HttpResponse
    # Create your views here.
    
    def test(request, bbb):
        print(bbb)
        print('test')
        return HttpResponse('test')
    """
    

    image

    image

    4. 反向解析

    # 反向解析其實就是:
    	通過一個路由別名 可以得到別名所在的這行路由的 完整地址
    	路由是可以起別名的
        
    '''以下都是直接訪問原始域名:127.0.0.1:8000'''
    # 1.后端反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^v1/v2/v3/v4/v5/test/', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入:
    """
    from django.shortcuts import HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 后端反向解析
        print(reverse('test'))
        return HttpResponse('home')
    """
    
    # 2.前端反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^test', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入:
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 前端反向解析
        print(reverse('test'))
        return render(request, 'home.html')
    """
    
    	# 新建home.HTML文件:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <link  rel="stylesheet">
        <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <a href="{% url 'test' %}">反向解析</a>
    </body>
    </html>
    
    
    # 3.后端無名帶參數的反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^test/(\d+)', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 后端反向解析
        print(reverse('test', args=(111, )))
        return HttpResponse('home')
    """
    
    
    # 4.后端有名帶參數的反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^test/(?P<aaa>\d+)', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 后端反向解析
        print(reverse('test', kwargs={'aaa': 1111}))
        return HttpResponse('home')
    """
    
    
    # 5.前端無名帶參數的反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^test/(\d+)', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 前端反向解析
        return render(request, 'home.html')
    """
    
    	# home.HTML文件:
    """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <link  rel="stylesheet">
        <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <a href="{% url 'test' 111 %}">反向解析</a>
    </body>
    </html>
    """
    
    
    
    # 6.前端有名帶參數的反向解析
    	# 在urls.py文件中寫入:
    """
    url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
    url(r'^test/(?P<aaa>\d+)', views.test, name='test'),
    """
    
    	# 在app目錄下的views.py文件中寫入
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def home(request):
        # 前端反向解析
        print(reverse('test', kwargs={'aaa': 111}))
        return render(request, 'home.html')
    """
    
    	# home.HTML文件:
    """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <link  rel="stylesheet">
        <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <a href="{% url 'test' aaa=111 %}">反向解析</a>
    </body>
    </html>
    """
    

    image

    image

    5. 路由分發

    # 重點
    	以后我們的django項目會越來越大,那么就會導致總路由文件會越來越大,就會非常難維護,我們會把總路由分開到不同的應用下面
    '''
    django的每一個應用都支持有自己的對應配置:
    	urls.py
    	templates
    	static
    '''
    
    # 使用路由分發一般都是多個應用
    	1.先再創建一個應用
    		startapp app02
    		創建完成后記得到settings.py中配置
        
    	2.將 urls.py文件復制一份到app01、app02目錄下
    
    
    # 實現路由分發方式一:
    # 訪問:127.0.0.1:8000/app01/index/
    # 訪問:127.0.0.1:8000/app02/index/
    	1.總路由 urls.py中寫入:
    """
    from django.conf.urls import url
    from django.contrib import admin
    
    
    from app01 import views
    from django.conf.urls import include
    from app01 import urls as app01_urls
    from app02 import urls as app02_urls
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
        url(r'^app01/', include(app01_urls)),
        url(r'^app02/', include(app02_urls))
    ]
    """
    
    	2.app01 urls.py文件中寫入:
    """
    from django.conf.urls import url
    from app01 import views
    
    urlpatterns = [
        url(r'^index', views.index),
    ]
    
    """
    
    	3.app02 urls.py文件中寫入:
    """
    from django.conf.urls import url
    from app02 import views
    
    urlpatterns = [
        url(r'^index', views.index),
    ]
    """
    
    	4.app01 views.py文件中寫入:
    """
    from django.shortcuts import HttpResponse
    # Create your views here.
    
    def index(request):
        return HttpResponse('app01:index')
    """
    
    	5.app02 views.py文件中寫入:
    """
    from django.shortcuts import HttpResponse
    # Create your views here.
    
    def index(request):
        return HttpResponse('app02:index')
    """
    
    
    # 實現路由分發方式二:
    	在總路由 urls.py中寫入:
    """
    from django.conf.urls import url
    from django.contrib import admin
    
    
    from app01 import views
    from django.conf.urls import include
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
        url(r'^app01/', include('app01.urls')),
        url(r'^app02/', include('app02.urls'))
    ]
    """
    

    image

    image

    6. 名稱空間

    # 當多個應用出現相同的別名時,反向解析就沒辦法識別了
    	1.在總路由urls.py文件中:
    """
    from django.conf.urls import url
    from django.contrib import admin
    
    from app01 import views
    from django.conf.urls import include
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.home),  # 客戶輸入默認域名 就是訪問首頁
        url(r'^app01/', include('app01.urls', namespace='app01')),
        url(r'^app02/', include('app02.urls', namespace='app02'))
    ]
    """
    
    	2.在app01的urls.py文件中:
    """
    from django.conf.urls import url
    from app01 import views
    
    urlpatterns = [
        url(r'^index', views.index, name='app01_index'),
        url(r'^home', views.home, name='index'),
    ]
    """
    
    	3.在app01的views.py文件中:
    """
    from django.shortcuts import render, HttpResponse, reverse
    # Create your views here.
    
    def index(request):
        return HttpResponse('app01:index')
    
    def home(request):
        print(reverse('app01:app01_index'))
        print(reverse('app02:app02_index'))
        return HttpResponse('home')
    """
    
    	4.在app02的urls.py文件中:
    """
    from django.conf.urls import url
    from app02 import views
    
    urlpatterns = [
        url(r'^index', views.index, name='app02_index'),
    ]
    """
    
    	5.在app02的views.py文件中:
    """
    from django.shortcuts import HttpResponse
    # Create your views here.
    
    def index(request):
        return HttpResponse('app02:index')
    """
    

    image

    7. 虛擬環境

    # 針對不同的項目,創建出來一個新的python環境,類似于是一個新的python解釋器
    

    image

    image

    posted on 2022-02-28 19:57  Deity_JGX  閱讀(4)  評論(0編輯  收藏  舉報

    国产在线码观看超清无码视频,人妻精品动漫H无码,十大看黄台高清视频,国产在线无码视频一区二区三区,国产男女乱婬真视频免费,免费看女人的隐私超爽,狠狠色狠狠色综合久久蜜芽