Django basics one

Getting started with Django 2.0.2

The latest version of Django is django2.0.2. Compared with the previous version, this version is not compatible with each other, and some new operations have been updated in the new version. We need to understand it. I plan to follow it here. Let's find out together.


1. Create a new folder named Test1, and then execute the command to create a django project

write picture description here

The first command is to create a project, and the second command is to create an application. After execution, we open it with PyCharm IDE. It is the following directory interface. Each of the following directory structures always represents a meaning. speak slowly
write picture description here

2. Run the project, develop the server python manage.py runserver If you open http://127.0.0.1:8000/ and the following page appears, it means that the first step of the django2.0.2 project is successful

write picture description here
write picture description here

3. Create a new template and associate the database MySql

1 Create a urls.py file in the application booktest, which is convenient for calling the url of the booktest application in the future, which is convenient and management. If it is directly placed in the root url, of course there is no problem, but the official also recommends that we put it in the urls under the application.
write picture description here

2 Write the matching situation in the url under the booktest application, which means calling the index function in the view without adding any parameters
write picture description here

3 Write the view here, that is, the content effect of the page. The following figure shows that it has been successful
write picture description here

Remarks: In view of the above situation, let me say something to pay attention to. Before the new version of django has made great improvements in matching urls, it is in the
form of url("^blog/$", view.index ), and now it is directly path(' blog', views.index) which means that regular expressions are not needed. Of course, if you think regular expressions are better, you can use re_path instead of path. Use regular matching as in the previous 1.8 version

4 Data association

  1. First create a database testfordjango;

write picture description here
2. Configure the database;
write picture description here
3. Migrate, that is, bind the django model to the database, create a table matching the model, and view the database file
write picture description here
write picture description here

From the above picture, there are many tables added to the database, two of which are indicated by one-to-one correspondence with the model we created. At this time, it means that our model and our database have been associated. Next, we can do the following developed step by step

It's too late to be here today, it will be updated soon in these two days...  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651201&siteId=291194637