Django 3.0 documentation notes --tutorial 01

Introduction

Currently on the market there is little tutorial Django 3.0, so according to their own documents, analysis and notes for future reference. And understand the important point will be recorded, for learning.

First stroke again tutorial, is the simple entry

https://docs.djangoproject.com/zh-hans/3.0/intro/tutorial01/

surroundings

Windows    /    pycharm    /    Python 3.7    /     Django 3.0

Writing your first Django application, Part 1

Project Creation

First, according to the document install Django 3.0, the project can be created directly in the pycharm

You can see the project structure

manage.py   various ways project management Django command-line tool

my_try   is a pure Python package, when we want to refer to where the urls, should be written as  my_try.urls

__init__.py    empty file, represented as a Python package

asgi.py    configuration asgi

settings.py    configuration (settings) file

urls.py    the URL of the statement Django project, as you site's "directory"

swgi.py    as run your project on the entrance WSGI-compliant Web server

Simple server for development

Confirm whether to create success

Can be directly run manage.py in pycharm

You can also use the command line 

py manage.py runserver

To this page and saw a small rocket, that is success

About the document hints:

你刚刚启动的是 Django 自带的用于开发的简易服务器,
它是一个用纯 Python 写的轻量级的 Web 服务器。
我们将这个服务器内置在 Django 中是为了让你能快速的开发出想要的东西,
因为你不需要进行配置生产级别的服务器(比如 Apache)方面的工作,
除非你已经准备好投入生产环境了。

现在是个提醒你的好时机:
千万不要 将这个服务器用于和生产环境相关的任何地方。
这个服务器只是为了开发而设计的。
(我们在 Web 框架方面是专家,在 Web 服务器方面并不是。)

Create a voting application

Projects and Applications    can be roughly understood as the project is a collection of configuration and application-specific sites, the project may contain multiple programs (in general)

命令行创建应用:

py manage.py startapp polls

startapp即为开始一个程序,polls为应用名

这个目录结构包括了投票应用的全部内容

编写第一个视图

创建视图

打开polls/views.py

代码(可看文档):

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    return HttpResponse("hello !")

index定义了一个视图,为request子类

Django使用request和response对象在系统间传递状态。

当一个页面被请示时,Django创建一个包含请求元数据的 HttpRequest 对象。然后Django调入合适的视图,把 HttpRequest 作为视图的函数的第一个参数传入。每个视图要负责返回一个 HttpResponse 对象。

上述的view,我们需要将一个 URL 映射到它

创建应用URL

在polls里创建一个urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index(), name='index'),
]

下一步是要在根 URLconf 文件中指定我们创建的 polls.urls 模块

加入项目URL

my_try/urls.py

在urlpatterns里增加一个include()

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
函数 include() 允许引用其它 URLconfs。
每当 Django 遇到 include() 时,它会截断与此项匹配的 URL 的部分,
并将剩余的字符串发送到 URLconf 以供进一步处理。

所以,当我们在网址中为 polls 时,在my_try/urls.py​​​​​​​中发现了polls/,又发现了include(),截断将剩下部分发送到polls的urlconf中(即include中的polls.urls),剩下部分为'',所以匹配到view.index()

即此处的外层的URL确定应用(polls),polls的URL确定视图

我们看到my_try/urls.py里还有一个 

path('admin/', admin.site.urls),

我们尝试输入一下

 可以看到时Django提供的一个管理后台

path函数

函数 path() 具有四个参数,两个必须参数:route 和 view,两个可选参数:kwargs 和 name

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

view   当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个 HttpRequest 对象作为第一个参数

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

发布了52 篇原创文章 · 获赞 21 · 访问量 4万+

Guess you like

Origin blog.csdn.net/zhr1030635594/article/details/104116049