由于我们要在新项目中处理Django,所以我将对其进行概述

客户端向特定文件发送“我想要这个页面”的请求,然后服务器将所需文件发送回客户端(响应)。
非常简单啊。

Django 的起步
$ python -m django --version
可以检查软件的版本。假设已经安装好了。

$ django-admin startproject mysite

移动到刚才创建的mysite文件夹后,运行以下命令。
python manage.py runserver


到目前为止,我注意到的一点
当执行manage.py时,与服务器建立了连接。
类似于Flask中的main.py。
app=run()
有一个文件。
根据上述图表来看,url.py应该对应于Flask的app.py。
根据代码来看,它似乎在进行路由设置。
我明白了路由,但关键的模板呢?
屏幕是如何显示的?是通过HTML之类的吗?
暂时保留疑问,试着继续前进。

什么?顺便说一下,views.py还没有被创建。

我会先试着按照所说的去执行。
$ python manage.py startapp polls


我会按照views.py中所说的进行记录。
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
而且,似乎还需要在poll文件夹下创建一个urls.py。
那么,前一级的东西是什么?
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

你在说什么!

当我运行了服务器并访问了/polls路径时,显示出了Index的内容!
这是到目前为止的总结。
这个文件的作用是定义URL吗?
在生成URL之后,与views.py中的方法进行了关联,并返回HttpResponse。
类似于Flask中的渲染模板功能。
当前的印象如下:
例如,在Flask中,
●app.py文件
使用Blueprint将路由函数按模块分开。
●views/test.py文件
是由Blueprint指向的地方。
实际的处理和路由处理都在这里。
主要的写作位置?
●main.py文件
通过执行app=run()来调用app.py文件。
当比较Flask和Django时,可以感受到它们的不同之处。
●urls.py → app.py
●URLs 的 Python 文件 → 应用程序的 Python 文件
●views.py
→视图文件夹中的路由处理部分
●manage.py
→main.py
入口文件。
关于尚未出现的”template”和”model.py”,可能在路由时会参考”template”。
“template”中应该包含类似Flask的templates,里面会编写HTML、CSS等来写入页面信息。
在与数据库进行交互时,我猜测您应该会实例化模型并以ORM的方式连接和操作数据库。
只要理解到这一点,后面只需要熟悉Django的编写方式就可以了。
下一个时代