Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of creating Python Django projects and applications

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the creation method of Python Django project and application". In daily operation, I believe that many people have doubts about the creation method of Python Django project and application. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the creation method of Python Django project and application?" Next, please follow the editor to study!

Create a Django project

Create a Django project named project and enter it on the cmd command line

Django-admin startproject project

Entering the command will generate a project package called project with a manage.py file and a project package with the same name as the project.

Manage.py file: a useful command line tool that allows you to interact with the Django project in a variety of ways.

The directory structure we can see in the project package:

Project/__init__.py: an empty file that tells Python that the directory is a Python package.

Project/settings.py: the setup / configuration of the Django project.

Project/urls.py: the URL declaration of the Django project; this is the root route of the project.

Project/wsgi.py: a portal to a WSGI-compatible Web server to run your project

Settings.py project profile

After the project is created, there is a settings.py file in the project directory of the project with the same name, which is used to configure and manage the operation and maintenance information of the Django project.

All configuration items in the settings.py configuration file are capitalized, and when the project is created, some default configurations are initialized, which carry the most basic project information.

The commonly used configuration items are:

DATABASES: database configuration

TEMPLATES: configure the template address of the HTML page templates

STATICFILES_DIRS: configuring static fil

MIDDLEWARE: configuring middleware

DEBUG: default is True, and change to False when the project is launched

ALLOWED_HOSTS: qualifies the host value in the request

Urls.py routing system

The function of the routing system of Django is to establish a mapping relationship between the function of processing data in views and the requested url.

After the request arrives, the processing method corresponding to the request is found according to the relationship entry in urls.py, and the data is returned to the client http page.

In the Django1.x version, the url mapping is generally a regular expression, the beginning of the "^" string and the end of the "$" string. Item entries are compared one by one from scratch, and as soon as a match is encountered, the view function or secondary route of the entry map is executed immediately, and subsequent entries will no longer continue to match. Therefore, the order in which url routes are written is crucial!

In Django1.x, a url mapping entry has at least two parameters, one is a regular rule, and the other is a view function. The code is as follows:

From django.conf.urls import url # url () method Import method replace url with re_path in url (r'^ test/$',views.test) # django2

In the Django2.x version, route mapping uses the path () or re_path () method. Path () does not have the regular rules and matches strings directly. The use of re_patah () is the same as url (). The use of path () is as follows:

The import method of from django.urls import path,re_path # path () method path ('test/',views.test) # does not require the use of regular rules such as ^ $

Note: for regular matching using re_path () in this course, you can choose either path () or re_path ().

When there are multiple applications (App), we will create a urls.py routing module in each App, and then forward all the url requests to which the app belongs to the corresponding urls.py module from the root route.

At this point, the study on "what is the method of creating Python Django projects and applications" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report