search
HomeBackend DevelopmentPython TutorialDetailed explanation of the entire process of building a Django project in Python

This article mainly introduces the whole process of building the Python web framework Django project. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look

The whole process of building Django project of Web framework in Python

IDE description:

  1. Win7 system

  2. Python: 3.5

  3. Django:1.10

  4. Pymysql:0.7.10

  5. Mysql:5.5

Note: You can view the installed library version information through pip freeze.

Django is a free open source website framework developed in Python, which can be used to quickly build high-performance, elegant websites!

Django features

  1. Powerful database functions

  2. Use python classesInheritance, with just a few lines of code you can have a rich, dynamic database operationInterface(API), you can also execute SQL if needed statement.

  3. Built-in powerful backend function

  4. A few lines of simple code will give your website a powerful backend, making it easy to manage your Content! Elegant URL

  5. Use regular expressions to match the URL, pass it to the corresponding function , and define it as you like!

  6. Template system – a powerful, easy-to-expand template system with simple design. Code and styles are designed separately, making it easier to manage.

  7. Caching system – Used in conjunction with memcached or other caching systems for better performance and faster loading speed.

  8. Internationalization – fully supports multi-language application, allowing you to define translated characters and easily translate into languages ​​of different countries.

The installation operations of Python and MySQL will not be explained here. You can find and solve them on the Internet. Among them, the installation of django and pymysql can be completed using the pip install * command.

After installing Django, you can use the django-admin.py management tool to create a project. First, let’s take a look at the command introduction of django-admin.py. Enter django-admin.py on the command line to view the available project management commands.

Detailed explanation of the entire process of building a Django project in Python

Django project The specific process of creating a HelloWorld project is as follows:

#Step1: Before building a Django project, first select the project Storage directory. Then CD switch to the project storage directory in the Dos window.

Step2: Create project Execute django-admin.py startproject HelloWorld

Open IDEA, you can see the created project directory as shown below :

Detailed explanation of the entire process of building a Django project in Python

Directory description:

  1. HelloWorld: The container of the project.

  2. manage.py: A useful command line tool that allows you to interact with this Django project in various ways.

  3. HelloWorld/init.py: An empty file that tells Python that the directory is a Python package.

  4. HelloWorld/settings.py: Settings/configuration for this Django project.

  5. HelloWorld/urls.py: URL declaration for this Django project; a "directory" of websites powered by Django .

  6. HelloWorld/wsgi.py: A portal to a WSGI-compatible web server to run your project.

Next we enter the HelloWorld directory and enter the following command to start the server:

python manage.py runserver 0.0.0.0:8000

0.0.0.0 allows other computers to connect to the development server, 8000 is the port Number. If not specified, the port number defaults to 8000.

Enter your server’s IP and port number in the browser. If it starts normally, the output will be as follows:

Detailed explanation of the entire process of building a Django project in Python

Step3: To create an application, enter django-admin.py startapp demo

on the command line. Open IDEA and you can see the created project directory as shown below:

Detailed explanation of the entire process of building a Django project in Python

Directory description:

  1. demo: 应用的容器。注:后面的页面设计文件,在此目录下创建目录templates,名为XX.html的文件放在此处。

  2. init.py:如上一个init.py文件

  3. migrations: 数据库相关目录,同步数据库之后会出现数据类。

  4. admin.py: admin后台管理文件

  5. apps.py: app应用管理文件

  6. models.py:主要用一个 Python 类来描述数据表,称为模型(model) 。运用这个类,你可以通过简单的 Python的代码来创建、检索、更新删除 数据库中的记录而无需写一条又一条的SQL语句。

  7. tests.py:测试文件

  8. views.py:包含了页面的业务逻辑。

创建超级管理员

python manage.py createsuperuser

# 按照提示输入用户名和对应的密码就好了邮箱可以留空,用户名和密码必填

# 修改 用户密码可以用:
python manage.py changepassword username

服务端响应客户端请求过程

流程图如下:

Detailed explanation of the entire process of building a Django project in Python 

上面的流程图可以大致描述Django处理request的流程,按照流程图2的标注,可以分为以下几个步骤:

1.用户通过浏览器请求一个页面。

2.请求到达Request Middlewares,中间件对request做一些预处理或者直接response请求。

3.URLConf通过urls.py文件和请求的URL找到相应的View。

4.View Middlewares被访问,它同样可以对request做一些处理或者直接返回response。

5.调用View中的函数。

6.View中的方法可以选择性的通过Models访问底层的数据。

7.所有的Model-to-DB的交互都是通过manager完成的。

8.如果需要,Views可以使用一个特殊的Context。

9.Context被传给Template用来生成页面。

a.Template使用Filters和Tags去渲染输出

b.输出被返回到View

c.HTTPResponse被发送到Response Middlewares

d.任何Response Middlewares都可以丰富response或者返回一个完全不同的response

e.Response返回到浏览器,呈现给用户

url() 函数

Django url() 可以接收四个参数,分别是两个必选参数:regex、view 和两个可选参数:kwargs、name,接下来详细介绍这四个参数。

  1. regex: 正则表达式,与之匹配的 URL 会执行对应的第二个参数 view。

  2. view: 用于执行与正则表达式匹配的 URL 请求。

  3. kwargs: 视图使用的字典类型的参数。

  4. name: 用来反向获取 URL。

Django项目部署

在前面的介绍中我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文后续将以 Nginx 为例。

设置用自己的iP地址访问项目

1.首先需要执行>manage.py runserver 0.0.0.0:8000。

2.在setting.py里面需要添加ALLOWED_HOSTS=”*”。

【相关推荐】

1. Python免费视频教程

2. Python学习手册

3. 马哥教育python基础语法全讲解视频

The above is the detailed content of Detailed explanation of the entire process of building a Django project in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools