Create sub-application
create
We can call the command line in the root directory of the project to create a sub-application:
# Common commands for creating sub-apps:
python manage.py startapp sub-app name
- python: python3 interpreter
- manage.py: When the project was created in the previous chapter, the file generated to manage the entire project
- startapp: instruction to create a sub-app
- Sub-application name: This name can be arbitrarily set by yourself, and will generally be specified according to the needs of the project.
In the demo project just created, you want to create a user users sub-application module, which can be executed:
# Enter the root directory of the project: cd ~ / Desktop / code / demo # Execute the command to generate a sub-application and generate a sub-application called users: python manage.py startapp users
After execution, you can see a subdirectory named users in the project directory.
Generally, a sub-application represents a module, for example: shopping cart module, payment module, product display module, etc ...
Sub-application catalog description
View the project directory at this time, the structure is as follows:
The role of each file in the generated sub-application:
The admin.py file is related to the background management site configuration of the website.
The apps.py file is used to configure information about the current sub-application.
The migrations directory is used to store database migration history files.
models.py file users save database model classes.
The tests.py file is used to develop test cases and write unit tests.
The views.py file is used to write web application views.
Configure sub-applications
Although the created sub-application directory file is placed in the project directory, the django project cannot immediately use the sub-application directly. It needs to be registered and installed before it can be used.
Default configuration information in settings
In the settings file, the default configuration information of the project in the installed_apps list:
Add sub-apps to settings
Configuring sub-application information is also known as: registering and installing a sub-application,
that is, adding the Config class in the sub-application's configuration information file apps.py to the INSTALLED_APPS list.
E.g:
Information about the apps.py file in the users sub-application just created
(users.apps.UsersConfig), added to INSTALLED_APPS, as shown:
Let the current total project know our newly added modules (sub-applications). This is convenient for project project calls
Create view
create
Open the users module you just created and write the view code in views.py.
# Import HttpRespose from django.http module from django.http Import the HttpResponse DEF index (Request): "" " index view : param request: the request comprises a request object information : return: response object " "" return the HttpResponse ( " Hello the world! " )
Note:
The first incoming parameter of the view function must be defined to receive the HttpReqeust object containing the request data constructed by Django, usually called request.
The return value of the view function must be a response object. You can put the string data to be returned in an HTTPResponse object.
Define routing URL
1) Create a new urls.py file in the sub-application to save the route of the application. Generally, we call the route in the sub-application: sub-route
The overall urls.py routing file in the project is called the total routing.
2) Define sub-routing information in the users.urls.py file
# Import from urls module re_path from django.conf.urls Import re_path # directory introducing our views from the current view of the module from . Import views # the urlpatterns django is identified automatically routing list of variables the urlpatterns = [ # each routing information needs to Use the url function to construct # url (path, view) re_path (r ' ^ index / $ ' , views.index), ]
3) Add data in the project's general routing demo / urls.py:
# Import include function from django.contrib Import ADMIN from django.urls Import path, include the urlpatterns = [ # Django have a default routing configuration information, the first row do not control: path ( ' ADMIN / ' , admin.site.urls) , # add the following route configuration information: path (R & lt ' Users / ' , the include ( ' users.urls ' )), ]
Use include to include the routing file (urls.py) in the sub-application users into the total route of the project. R ' ^ users / ' determines that all routes of the users sub-application start with / users /, such as the view index we just defined. , The final full access path is / users / index /. You can not write the total route here. For example: r '' , then the full path to our index is: / index /.
4) Start operation
Restart the django program
python manage.py runserver
Enter the URL http://127.0.0.1:8000/users/index/ in the browser to see the returned information