Django framework writes data to the database on the website

When writing web pages with django, the invocation of database content is indispensable. How to realize the interaction between the front-end and the back-end, this content felt difficult when I first contacted it, and now I will discuss it.
The name of the django database is: db.sqlide3
There is a file called models.py in the root directory of the project you created. This file requires you to create a class.
from django.db import models//This is equivalent to importing models from django,db to facilitate the following references.

class Goods(models.Model)://name a class called goods, the content in the brackets tells django to create a model
    type= models.CharField(max_length=10)//The middle field indicates that the longest text is 10
    name= models.CharField(max_length=5)//
    price= models.DecimalField(max_digits=100,decimal_places=2)//The field is talking about text integer and decimal places, and the decimal place is thousandths.
    unit=models.TextField(max_length=3)//The middle field indicates that the longest text is 3
    count = models.DecimalField(max_digits=100, decimal_places=0,default=1);//The default value is 1


You need to create a data table for your model in your database,
//In the terminal, enter the following statement in the virtual environment of your project
$ python manage.py makemigrations 'here is your app name'//This sentence is to prepare for migrating the database
Migrations for 'here is your app name':// these are the things that show up in the terminal
  0001_initial.py:
  - Create model Post
$ python manage.py migrate 'here is your app name'//this sentence is to migrate your database

Up to this point, there is already your template in the database, but there is no substantive content. What should we do?
//Open your admin.py file and put it in a folder with your models.py
from django.contrib import admin
from .models import Goods//Introduce your class

admin.site.register(Goods)//Register for your site

Here is to call the super background, first run the database in the terminal, then open the URL in the browser, and add /admin at the back so that you will jump to another interface, the super user interface.
//Don't worry, open your terminal and enter the following statement in the virtual environment
python manage.py createsuperuser//Create a super user
Username: admin//These are the prompts given to you by the terminal. Follow the prompts and you will be successfully created.
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

Now go to the super user page just now to log in, and then fill in the things according to the class you created. The things you fill in are the things that will appear in the database.
This is how to add your stuff to the database.






















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691591&siteId=291194637