“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

After a long time, I bid farewell to the damned front-end page learning (although there are a lot of magic tools, but I still need to know the basic HTML, CSS, JS, jquery and vue), and finally entered the server backend construction. After saying goodbye to socket and WSGC and Java version of the homemade humble HTTP server (Java version of simple WSGC), finally welcome the formal backend framework learning, although I intend to Java stack to the end but still too young to experience life with Python at this stage.

Django installation

Configure the virtual environment

It is recommended that you set up your virtual environment before using Django. The goal is to make your environment cleaner. For details on how to do this, see my previous blog: Blog Link. This blog describes how to build a virtual environment using virtualEnv and Virtualenvwapper. The effect is the same if you use PyCharm directly, as explained above, both in Linux and Windows. All you need to do is click on pyCharm. Of course, here’s how to use virtualenv directly under Windows, which was not covered in my previous blog post.

Create a project file. 2. Use the virtualenv command to create a virtual environment (the file name is env) 3. 4. Execute startup Script (start shutdown Script on shutdown)

Then specify the virtual environment in your IDE. Pycharm is a nice way to go.

Creating a Django project

The directory structure of a Django project

Their relationship is shown below:

Django-admin startProject projectName Creates an application django-admin startApp appnameCopy the code

After your application is created and registered, PyCharm may not create your django project by default, so the console will have to create it itself. In addition, PyCharm creates your virtual environment by default.

Project file diagram

First, take a look at the routing file, which handles the request path.When the route comes, execute the views function. Notice what I have here is the function under APP, the function is under viewsTemplate file The HTML file we read is in our template file.

Next comes the Models file, which is responsible for our processing of the database.

Multiple routing

We use this when our logic is complex and we have multiple routes in our projects. We can create routing files in the application.This is actually very similar to the main routing file. Then we go to the main route file declaration.So when you go to /Two/index you get a response.

Django database configuration

Activate the default SQllite

Django comes with a database as defaultNext, please use the database tool pyCharm, and vsCode please download the relevant plug-in (currently, my learning environment is Still Windows and I will transfer to Ubuntu when I finish the project. It is a good habit to write MD documents, I will keep recording and upload to CSDN).Find the position that the arrow points to. My time is 2018 Professional edition. If it is other versions, it may not be in this position. So now you have a table on your right, but that’s not all, you have to migrate. Input instruction (console)

python manage.py migrate
Copy the code

If all goes well, here’s what you can see:Then you can click on the table to view, change, etc., very convenient.

Switching database (SQL)

We can fix that in our Settings, but there are a couple of issues here.

        'ENGINE':'django.db.backends.mysql'.'NAME':'database name'.'USER':'username'.'PASSSWORD':'password'.'HOST':'host'.'PORT':'port'.Copy the code

Download the driver to install, here we must use Pymysql. But in use there is a problem to note, find the initialization file. Write this code and make it driver masquerade as MYSQLDBAnd then migrate,

python manage.py migrate
Copy the code

Simple Database operations

Create a table

This is handled in our Models.So now we’ve created the table, but we still have to do the mapping, which is to go through the file code

python manage.py makemigrations

Copy the code

This file is then generatedAnd then in migration

python manage.py migrate
Copy the code

CURD simple operation

So this is a straightforward demonstration of the process.

1. Find the table, the class that defines the table, in this case Student class 2. Students = Student() 3. Students. name = ‘jack’,students.name = ‘jack1’ Read students.object,all() read all students.object.get(pk=2 find id=2) 5. Stu1 = students.object.get(pk=2) stu1.name= ‘hello’ 6. Delete, query based, direct delete() stu1.delete() 7. Finally, all actions must be committed 8.students.save () or they will not be committed

supplement

Process shown in figure