Author: zifanwang Issued on May 7, 2020
explain
Most similar articles on the Internet have pits. The series of articles published on this site are super detailed and have no pits.
1. Create Django project
Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. C:\>django-admin startproject mysite C:\>
After successful creation, the directory structure is as follows:

2. Run Django server
You need to run Django manage.py This file, so you should cd to this file directory, and then enter the command:
python manage.py runserver
You can see on the console:
C:\mysite>python manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. May 05, 2020 - 12:50:49 Django version 2.1, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
It indicates that the server has been started successfully. We can open it in the browser http://127.0.0.1:8000/ The following interface is displayed:

3. Create and configure App function modules
stay manage.py Enter under the same level directory:
python manage.py startapp article
C:\mysite\>python manage.py startapp article C:\mysite\>
The creation is successful without any prompt You can see that there is an additional article folder, which is the app just created to place the article related code.
4. Register App
After creating the app, we should go to settings To register an app in py: Open MySite / settings Py found INSTALLED_APPS and add app:
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Add article 'article', ]
5. Configure access path (urls)
The url can be understood as the url link entered when visiting the website. Django knows how to locate the app only after the url is configured. Open URLs under mysite/mysite Py add the following code:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), # Add a new code to configure the url of the app path('article/', include('article.urls', namespace='article')), ]
Path is the routing syntax of Django. The parameter article / assigns the access path of the app; include distributes the path to the next step; namespace can ensure that a unique url is found, even if different app s use the same url (which will be used later). Remember to introduce include at the top.
Now we have distributed all accesses with the root path of article / to the article app through the path. However, apps usually have multiple page addresses, so the app also needs to have its own routing distribution, that is, article URLs.
There is no such file when the app is generated, so you need to create URLs in the article / folder yourself Py and enter:
from django.urls import path # The name of the app being deployed app_name = 'article' urlpatterns = [ # There are no urls yet ]
urlpatterns is temporarily empty. No path mapping is written. Don't worry. It will be written later. At this point, our app configuration is complete.