Introduction to Django framework and creating projects

1. Preliminary preparation

  • Open the command line of anaconda and activate the environment command in windows
venv\Scripts\activate

To close, enter deactivate

  • Install the Django web framework
pip install Django
  • Create a project from the command line
django-admin startproject blog

insert image description here
manage.py: the entry point for Django program execution
init.py : an empty file, telling Python that this directory should be considered as a Python package
settings.py: Django's overall configuration file, which can configure App, database, middleware, etc.

  • Go to the blog directory and execute the project
    manage.py is the entry point for Django program execution
python manage.py runserver

At this time, the server is connected.

2. Create an application

Django comes with tools that can generate the corresponding basic directory structure

  • Create an application article. In a project, the application is usually used to complete the tasks of different modules. A project can contain multiple applications. In Django, each application is a python package.
python manager.py startapp article

insert image description here
admin.py: configure the file of Django management background
model.py: create the file of database data model object

  • After the article is created, it will not take effect immediately, you need to activate the application in blogs/settings.py, the code is as follows:
    insert image description here

3. Data model format

  • Add data models to the application, mainly in articles/models.pyfile, create the corresponding class
from django.db import models  # 引入django.db.models模块


class User(models.Model):
    """
    User模型类,数据模型应该继承于models.Model或其子类
    """
    id = models.IntegerField(primary_key=True)  # 主键
    username = models.CharField(max_length=30)  # 用户名,字符串类型
    email = models.CharField(max_length=30)     # 邮箱,字符串类型

class Article(models.Model):
    """
    Article模型类,数据模型应该继承于models.Model或其子类
    """
    id = models.IntegerField(primary_key=True)  # 主键,IntegerField 整数值字段
    title = models.CharField(max_length=120)    # 标题,字符串类型
    content = models.TextField()                # 内容,文本类型
    publish_date = models.DateTimeField()       # 出版时间,日期时间类型
    user = models.ForeignKey(User, on_delete=models.CASCADE) # 设置外键

It contains the corresponding field types.

  • Perform a database migration, changing the default SQLite database to the more popular MySQL database.
DATABASES = {
    
    
    "default": {
    
    
        "ENGINE": "django.db.backends.mysql",
        "NAME":'mrsoft',#修改为你的数据库名称
        "USER":'ROOT',#修改为你的数据库用户名
        'PASSWORD':'ROOT'#修改为你的数据库密码
    }
}
  • Connect to your database in terminal
mysql -u root -p
  • Follow the prompts to enter the user name and password, and create the database after the connection is successful
create database mrsoft default character set utf8;
  • Install the MySQL database driver PyMySQL, the command is as follows:
pip install pymysql
  • Then add the following code at the beginning of the __init__.py file:
import pymysql
pymysql.version_info = (1,3,13,"final",0)
pymysql.install_as_MySQLdb()
  • Then execute the command to create the data table:
python manage.py makemigrations#生成迁移文件

insert image description here
The migration file is generated successfully.
You may encounter such a problem in the middle: RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods

pip3 install cryptography -i https://pypi.tuna.tsinghua.edu.cn/simple#清华大学源

Just install it.

Guess you like

Origin blog.csdn.net/weixin_46111970/article/details/127558746