天天看點

第09章節-Python3.5-Django目錄詳解 84、Django(django版本1.10.4)然後用pychram建立django程式方法:

4、Django(django版本1.10.4)

下載下傳開發包第三方鏡像(加速下載下傳)
pip install -i htps://pypi.douban.com/simple/ django ==1.10.4
解除安裝方法
pip uninstall django

(python3) C:\Users\Administrator>pip install django==1.10.4
Requirement already satisfied: django==1.10.4 in e:\evns\python3\lib\site-packages (1.10.4)
gerapy 0.8.5 has requirement django>=1.11, but you'll have django 1.10.4 which is incompatible.

(python3) C:\Users\Administrator>pip show django
Name: Django
Version: 1.10.4
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: http://www.djangoproject.com/
Author: Django Software Foundation
Author-email: [email protected]
License: BSD
Location: e:\evns\python3\lib\site-packages
Requires:
Required-by:
           
pip3 install django == 1.10.4

    
    
    C:\Python35\Scripts
    
    # 建立Django工程
    django-admin startproject 【工程名稱】
    
        mysite
            - mysite        # 對整個程式進行配置
                - init
                - settings  # 配置檔案
                - url       # URL對應關系
                - wsgi      # 遵循WSIG規範,uwsgi + nginx
            - manage.py     # 管理Django程式:
                                - python manage.py 
                                - python manage.py startapp xx
                                - python manage.py makemigrations
                                - python manage.py migrate
        
        
        
    # 運作Django功能
    python manage.py runserver 127.0.0.1:8001
    
    
    chouti
        - chouti
            - 配置
        - 主站 app
        - 背景管理 app
    
    
    
    # 建立app
    python manage.py startapp cmdb
    python manage.py startapp openstack
    python manage.py startapp xxoo....
    
    
    app:
        migrations     資料修改表結構
        admin          Django為我們提供的背景管理
        apps           配置目前app
        models         ORM,寫指定的類  通過指令可以建立資料庫結構
        tests          單元測試
        views          業務代碼
    
    
    
    1、配置模闆的路徑
    
        TEMPLATES = [
                {
                    'BACKEND': 'django.template.backends.django.DjangoTemplates',
                    'DIRS': [os.path.join(BASE_DIR, 'templates')],
                    'APP_DIRS': True,
                    'OPTIONS': {
                        'context_processors': [
                            'django.template.context_processors.debug',
                            'django.template.context_processors.request',
                            'django.contrib.auth.context_processors.auth',
                            'django.contrib.messages.context_processors.messages',
                        ],
                    },
                },
            ]
    2、配置靜态目錄
        static
    
        STATICFILES_DIRS = (
            os.path.join(BASE_DIR, 'static'),
        )

        
        <link rel="stylesheet" href="/static/commons.css" />
    

内容整理
    1. 建立Django工程
            django-admin startproject 工程名

    2. 建立APP
        cd 工程名
        python manage.py startapp cmdb

    3、靜态檔案
        project.settings.py
        
        STATICFILES_DIRS = (
            os.path.join(BASE_DIR, "static"),
        )
    
    4、模闆路徑
    
        DIRS ==>    [os.path.join(BASE_DIR,'templates'),]
        
    5、settings中
        
        middlerware
        
            # 注釋 csrf
            
            
    6、定義路由規則
        url.py
        
            "login" --> 函數名
            
    7、定義視圖函數
        app下views.py
            
            def func(request):
                # request.method   GET / POST
                
                # http://127.0.0.1:8009/home?nid=123&name=alex
                # request.GET.get('',None)   # 擷取請求發來的而資料
                
                # request.POST.get('',None)
                
                
                # return HttpResponse("字元串")
                # return render(request, "HTML模闆的路徑")
                # return redirect('/隻能填URL')
                
    8、模闆渲染
        特殊的模闆語言
        
            -- {{ 變量名 }}
        
                def func(request):
                    return render(request, "index.html", {'current_user': "alex"})
        
                    
                index.html
                
                <html>
                ..
                    <body>
                        <div>{{current_user}}</div>
                    </body>
                
                </html>
                
                ====> 最後生成的字元串
                
                <html>
                ..
                    <body>
                        <div>alex</div>
                    </body>
                
                </html>
            -- For循環
                def func(request):
                    return render(request, "index.html", {'current_user': "alex", 'user_list': ['alex','eric']})
        
                    
                index.html
                
                <html>
                ..
                    <body>
                        <div>{{current_user}}</div>
                        
                        <ul>
                            {% for row in user_list %}
                            
                                {% if row == "alex" %}
                                    <li>{{ row }}</li>
                                {% endif %}
                                
                            {% endfor %}
                        </ul>
                        
                    </body>
                
                </html>
                
            #####索引################# 
                def func(request):
                    return render(request, "index.html", {
                                'current_user': "alex", 
                                'user_list': ['alex','eric'], 
                                'user_dict': {'k1': 'v1', 'k2': 'v2'}})
        
                    
                index.html
                
                <html>
                ..
                    <body>
                        <div>{{current_user}}</div>
                        
                        <a> {{ user_list.1 }} </a>
                        <a> {{ user_dict.k1 }} </a>
                        <a> {{ user_dict.k2 }} </a>
                        
                    </body>
                
                </html>
            
            ###### 條件
            
                def func(request):
                    return render(request, "index.html", {
                                'current_user': "alex", 
                                "age": 18,
                                'user_list': ['alex','eric'], 
                                'user_dict': {'k1': 'v1', 'k2': 'v2'}})
        
                    
                index.html
                
                <html>
                ..
                    <body>
                        <div>{{current_user}}</div>
                        
                        <a> {{ user_list.1 }} </a>
                        <a> {{ user_dict.k1 }} </a>
                        <a> {{ user_dict.k2 }} </a>
                        
                        {% if age %}
                            <a>有年齡</a>
                            {% if age > 16 %}
                                <a>老男人</a>
                            {% else %}
                                <a>小鮮肉</a>
                            {% endif %}
                        {% else %}
                            <a>無年齡</a>
                        {% endif %}
                    </body>
                
                </html>
    

           
  • 然後用pychram建立django程式方法:

    • 點選 file > New Project 然後建立如下圖:

      image.png

  • 選擇Python的解析器(3.6) , 點選Create,
  • 得到以下目錄:
  • 然後點選(http://127.0.0.1:8000/)
  • 就能得到以下效果: