Django framework layer model of learning ----

Introduction of the model layer

Create a model

 

 

 Figure I'll blog application models.py file which creates an Article object with id, author, title, abstract, content, publish_time and other fields also defines a __str__ function returns the contents of the function django comes backstage management display the name of each piece of data, if we do not have this function data is difficult to distinguish when the background is displayed.

After creating the class Article need to execute two commands to activate the class to go inside the database

manage.py makemigrations Python - "generated migration file

manage.py the migrate Python - "Migrating to the database file synchronization

After completion of execution requires just generated background configuration table in admin.py file code is as follows:

 

 

Create a background user

As a completely configured, the next step is to create the background of a user.

Open cmd to our project which execute the command: Python manage.py createsuperuser

After performing enter the account password created can create a success

After starting the project access 127.0.0.1:8000/admin you can see a table we just created.

 

 

 Get information from the database is displayed

 

It is our database also create good, then you get inside the table data shows.

We now define a request function views.py inside info, as follows:

 

 

 There is an important step not forget that after the request to write good function, configure the routing as follows:

 

 Well, then you can configure see the following page in a browser to access 127.0.0.1:8000/blog/info:

 Problems encountered

After I re-created the user access 127.0.0.1:8000/admin background when the page can not be accessed, and the project I run to exit, but the other page is a normal visit. This situation is mostly Django and python version does not correspond caused my Python version 3.7 Django version corresponds to 2,0, but I downloaded version 3.0. After I uninstalled django re-install version of 2,0 can be a normal visit.

Guess you like

Origin www.cnblogs.com/humiao-0626/p/12609301.html