【读书笔记】《Django 5 By Example》阅读笔记:p1-p16

一、重要概念

Django的核心概念

可以说Model是Django处理数据的工具,Template是Django用于展示网页的工具,而View是连接二者的桥梁,将数据放入到Templates中去。而Django框架是控制器,利用Urls作为路由分发。

Main framework components

Django follows the MTV (Model-Template-View) pattern. It is a slightly similar pattern to the wellknown MVC (Model-View-Controller) pattern, where the template acts as the view and the framework itself acts as the controller. The responsibilities in the Django MTV pattern are divided as follows:

• Model: This defines the logical data structure and is the data handler between the database and the view.

• Template: This is the presentation layer. Django uses a plain-text template system that keeps everything that the browser renders.

• View: This communicates with the database via the model and transfers the data to the template for viewing.

The framework itself acts as the controller. It sends a request to the appropriate view, according to the Django URL configuration. When developing any Django project, you will always work with models, views, templates, and URLs. In this chapter, you will learn how they fit together.

Django Architecture架构

This is how Django handles HTTP requests and generates responses:

1. A web browser requests a page by its URL and the web server passes the HTTP request to Django.

2. Django runs through its configured URL patterns and stops at the first one that matches the requested URL.

3. Django executes the view that corresponds to the matched URL pattern.

4. The view potentially uses data models to retrieve information from the database.

5. Data models provide data definitions and behaviors. They are used to query the database.

6. The view renders a template (usually HTML) to display the data and returns it with an HTTP response.

Django APP django应用

django应用是项目里的子应用。类似谷歌站点下面的搜索,gmail,地图等,都是一个个独立的应用。

In Django, a project is considered a Django installation with some settings. An application is a group of models, views, templates, and URLs. Applications interact with the framework to provide specific functionalities and may be reused in various projects. You can think of a project as your website, which contains several applications, such as a blog, wiki, or forum, that can also be used by other Django projects.

二、动手操作

建立你的第一个项目

django-admin startproject mysite

建好后重点看看mysite目录,里面有settings.py和urls.py是我们后续需要重点看的。

初始化数据库

cd mysite

python manage.py migrate

运行服务器

python manage.py runserver

之后可以去127.0.0.1:8000看一下劳动成果。

创建应用

python manage.py startapp blog

posted @ 2026-10-05 12:51  viphhs  阅读(2)  评论(0)    收藏  举报