Quick preview of Django development knowledge points
If you want information related to the blog, follow the public account: chuanyeTry to get the relevant information!
Django development
1. Review of knowledge points:
-
Install Django
pip install django
-
Create a Django project
>>> django-admin startproject mysite(项目名)
Note: Pycharm can create. If created with Pycharm, remember to delete the DIR templates in settings.py.
-
Create app & register
>>>python manage.py startapp app01 >>>python manage.py startapp app02 >>>python manage.py startapp app03
INSTALLED_APPS = [ ... 'app01.apps.App01Config' ]
Note: Otherwise, when writing classes in models.py under the app, the table cannot be created in the database.
-
Configure the static file path & template path (place it in the app directory).
-
Configure database related operations (MySQL)
-
Third-party module (django3 version)
pip install mysqlclient
-
First go to MySQL to create a database yourself.
-
Configure database connection settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'gx_day16', # 数据库名字 'USER': 'root', 'PASSWORD': 'root123', 'HOST': '127.0.0.1', # 那台机器安装了MySQL 'PORT': 3306, } }
-
Written in models.py under app
from django.db import models class Admin(models.Model): """ 管理员 """ username = models.CharField(verbose_name="用户名", max_length=32) password = models.CharField(verbose_name="密码", max_length=64) def __str__(self): return self.username class Department(models.Model): """ 部门表 """ title = models.CharField(verbose_name='标题', max_length=32) def __str__(self): return self.title
-
Execute two commands:
>>>python manange.py makemigrations >>>python manange.py migrate
-
-
In urls.py, routing (correspondence between URL and function).
-
In views.py, view function, write business logic.
-
templates directory, write HTML templates (including template syntax, inheritance,
{% static 'xx'%}
) -
ModelForm & Form component, we develop the add, delete, modify and check function.
- Generate HTML tags (generate default values)
- Request data for verification.
- Save to database (ModelForm)
- Get error message.
-
Cookies, Sessions, and user login information are saved.
-
Middleware, which implements user authentication based on middleware, is based on:
process_request
. -
ORM operation //Modify the database
models.User.objects.filter(id="xxx")//对id进行筛选 models.User.objects.filter(id="xxx").order_by("-id")//排序
-
Pagination component.
2.Ajax request
For related knowledge points, please see Django series development
. Link: Django series development
https://blog.csdn.net/hellow_xqs/category_12535125.html?spm=1001.2014.3001.5482
3. Order
class Order(models.Model):
""" 订单 """
oid = models.CharField(verbose_name="订单号", max_length=64)
title = models.CharField(verbose_name="名称", max_length=32)
price = models.IntegerField(verbose_name="价格")
status_choices = (
(1, "待支付"),
(2, "已支付"),
)
status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)
admin = models.ForeignKey(verbose_name="管理员", to="Admin", on_delete=models.CASCADE)
When you want to get data from the database: object/dictionary
# 对象,当前行的所有数据。
row_object = models.Order.objects.filter(id=uid).first()
row_object.id
row_object.title
# 字典,{"id":1,"title":"xx"}
row_dict = models.Order.objects.filter(id=uid).values("id","title").first()
# queryset = [obj,obj,obj,]
queryset = models.Order.objects.all()
# queryset = [ {'id':1,'title':"xx"},{'id':2,'title':"xx"}, ]
queryset = models.Order.objects.all().values("id","title")
# queryset = [ (1,"xx"),(2,"xxx"), ]
queryset = models.Order.objects.all().values_list("id","title")
summary
So far, the addition, deletion, modification and check of pages have been implemented based on Ajax + dialog box.
- Forms can be added, deleted, modified and checked.
- Ajax, realize adding, deleting, modifying and checking.
chart
- highchart, abroad.
- echarts, domestic.
Chart learning links: Link: Reference documents
More reference documents: https://echarts.apache.org/handbook/zh/get-started