This is the 17th day of my participation in Gwen Challenge
The target
Knowledge of virtual environments, Django projects, manage.py directives, and configuration files.
Working with the Django framework gives you a thorough understanding of the development process and basic conceptual elements.
Create your first Django project, the Library Management System (BMSTest).
Preparing the development environment
A programming language | version |
---|---|
Python | 3.7.9 |
This blog installation tutorial is quite detailed: Python environment installation tutorial
Third-party libraries/frameworks | version | instructions |
---|---|---|
Django | 3.1.2 | Web Development Framework |
virtualenv | 20.0.35 | Virtual environment library |
virtualenvwrapper-win | 1.2.6 | Virtual Environment Extension Pack (Command convenience) |
After installing Python, use the Pip tool to install the Django framework and VirtualEnv
pip install Django
pip install virtualenv
Copy the code
If you want to specify the version
pip install Django==3.12.
pip install virtualenv==20.035.
pip install virtualenvwrapper-win==1.26.
Copy the code
The default source may be a bit slow to install the third library, you can configure other mirror sources. Pip installation of third-party libraries slow (Solution)
If you just want to temporarily install the third library faster, you can temporarily use another image source.
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Django==3.12.
Copy the code
The development tools | version |
---|---|
Pycharm | 2020.2.3 |
Windows CMD | / |
You can install PyCharm pro to develop your Django project
A virtual environment
Understanding virtual Environments
What if you want to develop multiple projects on the same machine and need to use different versions of the same package? If you use the command above, install or update in the same directory, the other projects must not run.
Solution: Virtual environments.
So what is a virtual environment?
Here is a simple example. There is a Word file on the desktop. When we open it and modify it for a while, we find that the original file is better. So what to do? One solution is to make a copy of the file before modifying it, and then make changes in the copy so that if you find errors in the changes, the original file will not be affected.
The virtual environment is essentially a copy of the real Pyhton environment, so that we can install packages in the copied Python environment without affecting the real Python environment. By creating multiple virtual environments, you can isolate projects in different virtual environments.
Virtual environment instruction
instruction | For example, | role |
---|---|---|
mkvirtualenv | Mkvirtualenv -p python3 Name of the virtual environment | Creating a Virtual Environment |
deactivate | deactivate | Exiting a Virtual Environment |
workon | workon | Viewing a Virtual Environment |
workon | Workon Indicates the name of a virtual environment | Using virtual Environments |
rmvirtualenv | Rmvirtualenv Virtual environment name | Deleting a Virtual Environment |
Note:
Deactivate: deactivate: rmVirtualenv Specifies the name of the virtual environmentCopy the code
Virtual Environment Creation
Create the PYTHon3 virtual environment py_django in a CMD window:
mkvirtualenv -p python3 py_django
Copy the code
It is recommended that you create a DjangoProject folder just to learn and practice your Django project.
Note:
The default virtual environment to be created is C:\Users\Administrator\Envs. Administrator represents the user that I am currently using on my computer. You may specify a path for storing the virtual environment under other Users. You can add the WORKON_HOME system variable to your computer’s system environment variables
Library Management System (BMSTest) project
Install the Django framework in a virtual environment
First, enter the Py_Django virtual environment
workon py_django
Copy the code
PIP then installs Django
pip install django
Copy the code
Finally, check whether the installation is successful
pip list
Copy the code
Create the BMSTest project
Django-admin startProject Project name Example: Django-admin startproject BMSTestCopy the code
Use the CMD dir command to see if the project was created successfully
PyCharm opens the project
Django project directory structure
Django project directory description
The file name | role |
---|---|
manage.py | Project management file through which to manage the project |
BMSTest | Directory with the same name as the project, in this case BMSTest |
__init__.py | An empty file that allows the directory BMSTest to be used as a package |
asgi.py | Django3.0 is a new asynchronous function module |
settings.py | The overall configuration file for the project |
urls.py | The URL configuration file for the project |
wsgi.py | Wsgi-compliant Web server entry for the project |
Creating the project application
Enter the Virtual Environment
Find the Terminal option in the lower left corner of Pycharm and turn it on
Then use the workon command to enter the virtual environment
PyCharm’s Terminal is the same as the CMD window.
Creating the Book app
Enter the command in the Terminal to create the Book application
python manage.py startapp book
Copy the code
Then refresh or wait and PyCharm will show you the app you created
Djangos application directory description
Folders/files | role |
---|---|
__init__.py | Is an empty file indicating that the current directory book can be used as a Python package |
migrations | Database Migration folder |
admin.py | Related to the backstage management of the website |
apps.py | Django generates the name of the app file |
models.py | Database operation related |
tests.py | For developing test cases, there will be dedicated testers in the actual development |
views.py | Receives the browser request, processes it, and returns the page relevant |
Install the application
Find INSTALLED_APPS in setting.py under BMSTest and add the Django application you just created (book) after it.
Run the Django project on the Web server
During development, Django provides a lightweight Web server written in pure Python to be used only during development, in order to quickly preview the effects of development.
Enter the following command in the PyCharm Terminal to run the server:
Python manage.py runserver IP: port For example, run python manage.py runserverCopy the code
You can omit the IP address and port. The default IP address is 127.0.0.1 and the default port is 8000.
Click the blue url or copy the url to open it in your browser to display the Django project’s default home page.
To end the server, press Ctrl + C on the PyCharm Terminal
The Chinese language and Chinese time zone are configured
Find LANGUAGE_CODE and TIME_ZONE in the BMSTest project setting. Py and set it to:
LANGUAGE_CODE = 'zh-hans' Configure the Chinese language
TIME_ZONE = 'Asia/Shanghai' # Set China time zone
Copy the code
Once you’re done setting up and running the server, your Django project’s home page becomes Chinese.
The public,
Create a new folder X
Nature took tens of billions of years to create our real world, while programmers took hundreds of years to create a completely different virtual world. We knock out brick by brick with a keyboard and build everything with our brains. People see 1000 as authority. We defend 1024. We are not keyboard warriors, we are just extraordinary builders of ordinary world.