Django是当前最出色基于Python的Web应用开发平台之一,本文对其进行简要介绍并创建一个空的Django工程。
Django 是一个开放源代码的 Web 应用框架,由 Python 写成,其主要目的是简便、快速的开发数据库驱动的网站。它强调代码复用,多个组件可以很方便的以“插件”形式服务于整个框架,Django有许多功能强大的第三方插件,你甚至可以很方便的开发出自己的工具包。
Django 开发组无比勤奋,版本计划很紧凑,迭代很频繁
Release Series | Latest Release | End of mainstream support1 | End of extended support2 |
---|---|---|---|
4.2 LTS | April 2023 | December 2023 | April 2026 |
4.1 | August 2022 | April 2023 | December 2023 |
4.0 | December 2021 | August 2022 | April 2023 |
3.2 LTS | April 2021 | December 2021 | April 2024 |
3.1 | 3.1.5 | April 2021 | December 2021 |
3.0 | 3.0.11 | August, 2020 | April, 2021 |
2.2 LTS | 2.2.17 | December 2, 2019 | April 2022 |
2.1 | 2.1.15 | April 1, 2019 | December 2, 2019 |
2.0 | 2.0.13 | August 1, 2018 | April 1, 2019 |
1.11 LTS 3 | 1.11.29 | December 2, 2017 | April 1, 2020 |
1.10 | 1.10.8 | April 4, 2017 | December 2, 2017 |
1.9 | 1.9.13 | August 1, 2016 | April 4, 2017 |
1.8 LTS | 1.8.19 | December 1, 2015 | April 1, 2018 |
1.7 | 1.7.11 | April 1, 2015 | December 1, 2015 |
1.6 | 1.6.11 | September 2, 2014 | April 1, 2015 |
1.5 | 1.5.12 | November 6, 2013 | September 2, 2014 |
1.4 LTS | 1.4.22 | February 26, 2013 | October 1, 2015 |
1.3 | 1.3.7 | March 23, 2012 | February 26, 2013 |
Django 版本 | Python 版本 |
---|---|
1.8 | 2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5 |
1.9, 1.10 | 2.7, 3.4, 3.5 |
1.11 | 2.7,3.4,3.5,3.6,3.7 (1.11.17添加) |
2.0 | 3.4,3.5,3.6,3.7 |
2.1,2.2 | 3.5, 3.6, 3.7 |
3.0 | 3.6, 3.7, 3.8 |
3.1 | 3.6, 3.7, 3.8 or later |
pip install Django
此时pip默认按装的是3.0.5版本
pip install Django==3.1.5
pip show
命令查看包信息:pip show django
-->
Name: Django
Version: 3.0.5
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
Location: d:\program_files\anaconda3\lib\site-packages
Requires: pytz, asgiref, sqlparse
或
python -m django --version
-->
3.0.5
假设我们想要创建名为
vvd_django
的工程:
django-admin startproject vvd_django
tree /F # windows
tree -a # Linux
-->
└─vvd_django
│ manage.py
└─vvd_django
asgi.py
settings.py
urls.py
wsgi.py
__init__.py
vvd_django/
根目录只是你项目的容器, 根目录名称对Django没有影响,你可以将它重命名为任何你喜欢的名称。manage.py
: 一个让你用各种方式管理 Django 项目的命令行工具。你可以阅读 django-admin and manage.py 获取所有 manage.py
的细节。vvd_django/
目录包含你的项目,它是一个纯 Python 包。它的名字就是当你引用它内部任何东西时需要用到的 Python 包名。 (比如 vvd_django.urls
).vvd_django/__init__.py
:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。如果你是 Python 初学者,阅读官方文档中的 更多关于包的知识。vvd_django/settings.py
:Django 项目的配置文件。如果你想知道这个文件是如何工作的,请查看 Django 配置 了解细节。vvd_django/urls.py
:Django 项目的 URL 声明,就像你网站的“目录”。阅读 URL调度器 文档来获取更多关于 URL 的内容。vvd_django/asgi.py
:作为你的项目的运行在 ASGI 兼容的Web服务器上的入口。阅读 如何使用 WSGI 进行部署 了解更多细节。vvd_django/wsgi.py
:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。阅读 如何使用 WSGI 进行部署 了解更多细节。Django默认数据库为db.sqlite3
cd vvd_django
python manage.py migrate
-->
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
python manage.py runserver
-->
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 23, 2021 - 15:42:03
Django version 3.0.5, using settings 'vvd_django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
出现小火箭说明我们的第一个Django工程正常运作。
python manage.py startapp hello_world
│ db.sqlite3
│ manage.py
├─hello_world
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ └─migrations
│ __init__.py
└─vvd_django
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
└─ __init__.py
打开
hello_world/views.py
,把下面这些 Python 代码输入进去:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the vvd hello_world index.")
这是 Django 中最简单的视图。如果想看见效果,我们需要将一个 URL 映射到它
hello_world
目录里新建一个 urls.py
文件,并输入代码:from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
hello_world.urls
模块。在 vvd_django/urls.py
文件的 urlpatterns
列表里插入一个 include()
, 如下:from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('hello_world/', include('hello_world.urls')),
path('admin/', admin.site.urls),
]
函数
include()
允许引用其它 URLconfs, 它的理念是使其可以即插即用。每当 Django 遇到include()
时,它会截断与此项匹配的 URL 的部分,并将剩余的字符串发送到 URLconf 以供进一步处理。他们能够被放在任何路径下,使得应用都能够正常工作。
python manage.py runserver
serever/hello_world
链接——http://localhost:8000/hello_world/