7 月 152021
 

创建项目

$ django-admin startproject mysite

项目目录及文件说明

最外层的 mysite/ 根目录只是你项目的容器, 根目录名称对 Django 没有影响,你可以将它重命名为任何你喜欢的名称。
manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。
里面一层的 mysite/ 目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如 mysite.urls).
mysite/__init__.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。
mysite/settings.py:Django 项目的配置文件。
mysite/urls.py:Django 项目的 URL 声明,就像你网站的“目录”。
mysite/asgi.py:作为你的项目的运行在 ASGI 兼容的 Web 服务器上的入口。
mysite/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。

启动用于开发的简易服务器

$ python manage.py runserver

更换端口

$ python manage.py runserver 8080
$ python manage.py runserver 0:8000

创建应用

$ python manage.py startapp polls

函数 include() 允许引用其它 URLconfs。每当 Django 遇到 include() 时,它会截断与此项匹配的 URL 的部分,并将剩余的字符串发送到 URLconf 以供进一步处理。
当包括其它 URL 模式时你应该总是使用 include() , admin.site.urls 是唯一例外。

函数 path() 具有四个参数,两个必须参数:route 和 view,两个可选参数:kwargs 和 name。现在,是时候来研究这些参数的含义了。

path() 参数: route
route 是一个匹配 URL 的准则(类似正则表达式)。当 Django 响应一个请求时,它会从 urlpatterns 的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。

这些准则不会匹配 GET 和 POST 参数或域名。例如,URLconf 在处理请求 https://www.example.com/myapp/ 时,它会尝试匹配 myapp/ 。处理请求 https://www.example.com/myapp/?page=3 时,也只会尝试匹配 myapp/。

path() 参数: view
当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个 HttpRequest 对象作为第一个参数,被“捕获”的参数以关键字参数的形式传入。稍后,我们会给出一个例子。

path() 参数: kwargs
任意个关键字参数可以作为一个字典传递给目标视图函数。本教程中不会使用这一特性。

path() 参数: name
为你的 URL 取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。这个有用的特性允许你只改一个文件就能全局地修改某个 URL 模式。
10 月 192020
 

安装Django(提示已安装)

PS D:\Python> python.exe -m pip install Django
Requirement already satisfied: Django in c:\users\harveymei\appdata\local\programs\python\python39\lib\site-packages (3.1.2)
Requirement already satisfied: asgiref~=3.2.10 in c:\users\harveymei\appdata\local\programs\python\python39\lib\site-packages (from Django) (3.2.10)
Requirement already satisfied: sqlparse>=0.2.2 in c:\users\harveymei\appdata\local\programs\python\python39\lib\site-packages (from Django) (0.4.1)
Requirement already satisfied: pytz in c:\users\harveymei\appdata\local\programs\python\python39\lib\site-packages (from Django) (2020.1)
PS D:\Python> 

查看Django版本信息

import django
print(django.get_version())

PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
3.1.2
PS D:\Python>

命令行

PS D:\Python> python.exe -m django --version
3.1.2
PS D:\Python>

PS D:\Python> python.exe -m pip show django
Name: Django
Version: 3.1.2
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD-3-Clause
Location: c:\users\harveymei\appdata\local\programs\python\python39\lib\site-packages
Requires: sqlparse, pytz, asgiref
Required-by:
PS D:\Python>

创建项目

PS D:\Python> django-admin.exe startproject mysite
PS D:\Python> 

目录结构

mysite/             #根目录可以任意修改
    manage.py       #管理Django项目的命令行工具
    mysite/         #包名
        __init__.py #空文件,告诉Python将该目录当作python包
        settings.py #项目配置文件
        urls.py     #项目URL声明
        asgi.py     #项目运行在ASGI兼容服务器的入口
        wsgi.py     #项目运行在WSGI兼容服务器的入口

启动Django内置轻量级Web服务器

PS D:\Python> cd .\mysite\
PS D:\Python\mysite> python.exe .\manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 19, 2020 - 11:43:15
Django version 3.1.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK.

指定监听端口

PS D:\Python\mysite> python.exe .\manage.py runserver 8080
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 19, 2020 - 11:53:53
Django version 3.1.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CTRL-BREAK.
[19/Oct/2020 11:54:05] "GET / HTTP/1.1" 200 16351
[19/Oct/2020 11:54:06] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[19/Oct/2020 11:54:06] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[19/Oct/2020 11:54:06] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[19/Oct/2020 11:54:06] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692    
Not Found: /favicon.ico
[19/Oct/2020 11:54:06] "GET /favicon.ico HTTP/1.1" 404 1972

指定监听端口和IP(0是0.0.0.0的简写)

PS D:\Python\mysite> python.exe .\manage.py runserver 0:8080
7 月 282020
 

CentOS 8

编译报错(提示为nghttp2版本太旧)

[root@test httpd-2.4.43]# ./configure --enable-http2

checking for nghttp2... checking for user-provided nghttp2 base directory... none
checking for pkg-config along ... checking for nghttp2 version >= 1.2.1... FAILED
configure: WARNING: nghttp2 version is too old

启用仅CentOS 8支持的Power Tools软件源仓库

可供 CentOS 使用的软件库
https://wiki.centos.org/zh/AdditionalResources/Repositories

PowerTools —— 只供 CentOS8 使用,PowerTools 软件库提供了大量开发者用的工具。缺省是停用的。

[root@test httpd-2.4.43]# dnf config-manager --set-enabled PowerTools
[root@test httpd-2.4.43]# dnf makecache
CentOS-8 - AppStream                                           9.6 kB/s | 4.3 kB     00:00
CentOS-8 - Base                                                8.6 kB/s | 3.9 kB     00:00
CentOS-8 - Extras                                              4.4 kB/s | 1.5 kB     00:00
CentOS-8 - PowerTools                                          1.7 MB/s | 1.9 MB     00:01
Extra Packages for Enterprise Linux Modular 8 - x86_64          57 kB/s |  18 kB     00:00
Extra Packages for Enterprise Linux 8 - x86_64                  58 kB/s |  18 kB     00:00
Metadata cache created.
[root@test httpd-2.4.43]#

安装开发库

[root@test httpd-2.4.43]# dnf -y install libnghttp2-devel

===============================================================================================
 Package                   Architecture    Version                   Repository           Size
===============================================================================================
Installing:
 libnghttp2-devel          x86_64          1.33.0-3.el8_2.1          PowerTools           60 k

Transaction Summary
===============================================================================================
Install  1 Package

重新编译

[root@test httpd-2.4.43]# ./configure --enable-http2

checking whether to enable mod_http2... checking dependencies
checking for OpenSSL... (cached) yes
  setting MOD_LDFLAGS to "-lssl -lcrypto    -lpthread -ldl"
  setting MOD_CFLAGS to ""
  setting MOD_CPPFLAGS to "-DH2_OPENSSL"
checking for nghttp2... checking for user-provided nghttp2 base directory... none
checking for pkg-config along ...   setting MOD_CFLAGS to ""
checking for nghttp2 version >= 1.2.1... OK
  adding "-lnghttp2" to MOD_LDFLAGS
  setting LIBS to "-lnghttp2    -lpthread -ldl"
checking nghttp2/nghttp2.h usability... yes
checking nghttp2/nghttp2.h presence... yes
checking for nghttp2/nghttp2.h... yes
checking for nghttp2_session_server_new2... yes
checking for nghttp2_stream_get_weight... yes
checking for nghttp2_session_change_stream_priority... yes
  adding "-DH2_NG2_CHANGE_PRIO" to MOD_CPPFLAGS
checking for nghttp2_session_callbacks_set_on_invalid_header_callback... yes
  adding "-DH2_NG2_INVALID_HEADER_CB" to MOD_CPPFLAGS
checking for nghttp2_session_get_stream_local_window_size... yes
  adding "-DH2_NG2_LOCAL_WIN_SIZE" to MOD_CPPFLAGS
yes
  setting MOD_HTTP2_LDADD to "-export-symbols-regex http2_module"