Getting started with Django
Take the "books-heroes" management as an example to use Django to develop the basic process and learn the main knowledge of Django.
Install Django
pip install django
Note: When you use the pip install django command for installation, the old version is automatically deleted and the new version is installed.
Create a project
Command: django-admin startproject test1
Note: In the above command, "test1" is the name of the project you want to create.
Go to the test1 directory. The directory structure is as follows:
Note:
- Manage. py: a command line tool that allows you to interact with Django projects in multiple ways
- Test1: Real Python package of the project
- _ Init _. py: An empty file, which is required by the Python package.
- Settings. py: project configuration file
- Urls. py: project URL Declaration
- Wsgi. py: entry to the Web server compatible with WSGI
Design Introduction
This project maintains the information of "books-heroes" and stores two types of data: Books and heroes.
Structure Design of books and tables:
Table Name: BookInfo
Book name: btitle
Book Release Date: bpub_date
Hero table structure design:
Table Name: HeroInfo
Hero name: hname
Gender: hgender
Hero Introduction: hcontent
Book: hbook
Book-hero relationship: one-to-many
Database Configuration
In the settings. py file, use the DATABASES item to set the database.
# Database# https://docs.djangoproject.com/en/1.11/ref/settings/#databasesDATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }}
Django supports mainstream databases such as sqlite and mysql.
Django uses the sqlite database by default.
Create an application
One or more applications can be created in a project, and each application performs one business operation.
Command for creating an application: python manage. py startapp booktest
Note: The "booktest" in the above command is the name of the application you want to create
The application directory structure is as follows:
Define model end
A data table corresponds to a model class.
Open the models. py file and define the model class.
Import package from django. db import models
The Model class is inherited from the models. Model class.
Note: you do not need to define a primary key column. It will be automatically added during production and the value will automatically increase.
When an object is output, the str method of the object is called.
# Booktest/models. pyfrom django. db import models "Table Structure Design: Table Name: BookInfo book name: btitle book release date: bpub_date hero table structure design: Table Name: HeroInfo hero name: hname hero gender: hgender hero Introduction: hcontent book: hbook book-the relationship between heroes is one-to-many "" # Create your models here. # Library class BookInfo (models. model): btitle = models. charField (max_length = 20) bpub_date = models. dateTimeField () def _ str _ (self): return self. btitle # HeroInfo (models. model): hname = models. charField (max_length = 20) hgender = models. nullBooleanField () hcontent = models. charField (max_length = 1000) hbook = models. foreignKey ('bookinfo') def _ str _ (self): return self. hname
Generate data table
Activate model: edit the settings. py file and add the booktest application to INSTALLED_APPS.
# Application definitionINSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'booktest',]
Generate a migration file: generate an SQL statement: python manage. py makemigrations
The migration file is generated to the migrations directory of the application.
Execute migration: Execute an SQL statement to generate a data table: python manage. py migrate
Test data operations
Go to python shell and perform simple model API exercises: python manage. py shell
After entering the shell, the prompt is as follows:
# Introduce the required package:
From booktest. models import BookInfo, HeroInfo
From datetime import *
# Creating a book
B = BookInfo ()
B. btitle = "Legend of the legend"
B. bpub_date = datetime (year = 1990, month = 1, day = 10)
B. save ()
# Query information of all books:
BookInfo. objects. all ()
# Query the book information based on the conditions (the primary key is the primary key)
B = BookInfo. objects. get (pk = 1)
# Output book information
B
B. id
B. btitle
# Modifying book information
B. btitle = "tianlong Babu"
B. save ()
# Deleting Book Information
B. delete ()
Associated object operations
HeroInfo can be operated as described above
Add. Note: add the associated object.
H = HeroInfo ()
H. htitle = "Guo Jing"
H. hgender = True
H. hcontent = ""
H. hbook = B
H. save ()
To be continued ......