[Nanny level tutorial] Django configuration under windows system (3000 words in detail)

After reading a few Django books, I feel that there are always some things that I don’t understand about the configuration of Django in the windows system 3 in the book. Doing a small tutorial by myself can be regarded as sorting out.

1. Initialize a virtual space in the specified folder

Enter the specified folder, place the cursor in the address bar, enter cmd, press Enter, open cmd in this path, and enter

python -m venv ENV

Create a new virtual space, where
python: use python
-m: use script script
venv: build virtual space
ENV: virtual space name

After executing the command, a folder called ENV is generated in the folder where the command is executed. This is our virtual space
insert image description here

2. Activate the virtual space

enter

ENV\Scripts\activate

Activate the virtual space
insert image description here
After the activation is successful, there should be (space name) in front of our path.
When we want to close the virtual space, enter

deactivate

3. Install Django

enter

pip install django

Waiting for django to be installed

At this time, there are the following files in the ENV folder.
insert image description here
Include is empty.
In Lib, there is a site-package folder, including libraries such as pip, django, and sql.
Scripts contain exe and bat files to be used for startup.
pyvenv.cfg is the basic configuration of the environment, including the absolute path of the python program, etc.

4. Create a project in Django

enter:

django-admin startproject test1

django-admin is a command-line management tool of django, which can operate the project. Here we use the startproject command to create a new project named test1.
A folder called test1 will appear at the same level as the ENV folder.
A folder called test1 will appear at the same level as the ENV folder.
There is another test1 directory and manage.py file
Another test1 directory and manage.py file appear in the test1 directory
manange.py file in the test1 directory to receive commands and hand them over to django to run (similar to the role of django-admin we just used, the next two commands are very large in python3 The extent is equivalent)
The following 5 files
The following 5 files are generated in test1
" init.py " are generated in test1: now it is empty, used to initialize
asgi.py: the full name (Asynchronous Server Gateway Interface) is a set of interface standard protocol as opposed to WSGI / specification, used to standardize the connection between the web server and the python web or python framework. The difference from WSGI is that it better supports communication between asynchronous python applications and servers. It is provided here for developers and can be designed on demand , Not used much at the beginner stage.
settings.py: An important file in the development of beginners, setting how Django interacts with the system and how to manage projects.
urls.py: full name: uniform resource locator. Uniform resource locators, used here to tell Django which web pages should be created to respond to browser needs.
wsgi.py: full name: Python Web Server Gateway Interface, which is synchronous, a set of connections between the python web server and the python application. Interface standard protocol/specification.

5. Create a database

enter

cd test1
python manage.py migrate

cd test1; enter the test1 folder, because our manage.py file is in the test1 folder
python manage.py: run the mannage program. Here we just talked about it, which is used to receive commands and hand them over to Django to run.
migrate: The meaning of migration, which means modification when used on the database. When a new project using SQLite migrates the database for the first time, Django will create a new database.
After running, a file named db.sqlite3 appeared in the test1 file home.
insert image description here

6. Verify that the project was created correctly

6.1 runserver run project

enter

python manage.py runserver

Let the project run with the runserver command

6.1.1 Port occupation error solution

There may be an error in the middle

That port is already in use

This is because the port is occupied. It can be solved by closing the program that occupies the port or changing the port.
Method 1: Close the program that occupies the port
Open cmd, enter

netstat ano|findstr 8000

netstat: show socket content
-ano:
a -all: show everything about the socket
n-number: show address and port number in numeric form
o-operate: show owning process associated with each connection
|: Pipeline commands, use the parameters of the left command as the interface of the right command and use
findstr 8000: find the character 8000.
This operation will display the program that occupies the 8000 port (usually Kugou Music in China, don’t ask me how to know), remember the program’s pid in the figure is 9760
PID: Process Identification, process identifier
insert image description here

enter

taskkill /pid 9760 /f

taskkill: the command to terminate the process
/pid 9760: the pid of the process
/f-force: the forced execution
will terminate the process
Method 2: Change the port of Django runserver
Enter in our virtual environment:

python manage.py 8001

By analogy, it can also be replaced with other ports until the port is replaced successfully

6.2 Open the corresponding web page

After the runserver is successful, the following information will be displayed,
insert image description here
which means that the project is listening for requests on the corresponding port of your computer, open the browsing area and enter the address.
insert image description here
The appearance of Django's little rocket means that you're done.
Move the cursor to cmd and press ctrl+c to exit.

7. Create app

Enter in the virtual space cm

python manage.py startapp tests

Create an app
folder named tests A folder called tests appears under test, which contains the following file
insert image description here
migrations: data migration, with a blank init file
init: initialize
admin: create user
apps.py: store the basics of the app Information
model.py: defines the files to be managed in our development
tests.py: places test
views.py: accepts web requests and responds to web requests

Guess you like

Origin blog.csdn.net/weixin_45980989/article/details/125947157