Create a Django project
To create a Django project called project, at the cmd command line type
django-admin startproject project
After entering the command will generate a project package named project, a file inside the package and the project name of the same name project package.
-
File: A useful command-line tool that lets you interact with this Django project in a variety of ways.
Inside the project package we can view the directory structure:
-
project/__init__.py
: An empty file that tells Python that the directory is a Python package. -
project/
: The settings/configuration for this Django project. -
project/
: URL declaration for this Django project; this is the root route for the project. -
project/
: a WSGI-compliant web server entry point to run your project
Project Configuration File
After you create a project, there is a file in the project directory with the same name that is used to configure and manage the Django project's operation and maintenance information.
All configuration items in the configuration file are in uppercase. When the project is created, some default configurations are initialized, and these default configurations carry the most basic project information.
Among the commonly used configuration items are:
-
DATABASES
: Database configuration -
TEMPLATES
: Configure the template address of the HTML page templates -
STATICFILES_DIRS
: Configure static files -
MIDDLEWARE
: Configuration middleware -
DEBUG
: defaults to True, changes to False when the project goes live -
ALLOWED_HOSTS
: Qualify the host value in the request
routing system
Django's routing system works by mapping the functions that process data in views to the requested url.
After the request arrives, the processing method corresponding to the request is found based on the relationship entries in the request, and the data is returned to the client http page.
The url mapping in the version is generally a regular expression with a "^" string at the beginning and a "$" string at the end. The entries are compared one by one from the beginning, and as soon as a match is encountered, the view function or secondary route mapped to that entry is executed, and no further matches will be made for subsequent entries. Therefore, the order in which the url routes are written is critical!
In , a url mapping entry has at least two parameters, a regular rule and a view function, the code is as follows:
from import url # url() method import method url(r'^test/$',) # django2hit the nail on the headre_pathinterchangeabilityurl
In version routing mapping use path() or re_path() method, path() without the rules of regular those rules, directly match the string, re_patah() use the same as url(), path() is applied as follows:
from import path,re_path # path() method imports path('test/',) # No need to use ^ $ iso-regularity rule
Note: Regular matching with re_path() is used in this course with your choice of path() or re_path().
When there are multiple applications (apps), we will create a routing module in each app, and then from the root route, the url requests belonging to the app will all be forwarded to the corresponding module.
summarize
That's all for this post, I hope it was helpful and I hope you'll check back for more from me!