Django development: build virtual environment and projects, build data tables

Task 1: Build a virtual environment and project, and build a data table

1. Create a Django project and create an empty folder in the specified directory. I named it here My_Django.

2. Then create a virtual environment in Anaconda Prompt, here I named itdjango

3. First activate the newly created virtual environment

`conda activate django`

djangoThen install necessary packages such as django in the current virtual environment
pip install django
pip install djangorestframework
pip install django-filter
pip install drf_spectacular

4. Switch the Anaconda Prompt working directory to My_Djangothe folder and create the project

(base) C:\Users\Rongyao>conda activate django

(django) C:\Users\Rongyao>cd ..

(django) C:\Users>cd ..

(django) C:\>d:

(django) D:\>cd D:\常用文件\CQU\就业\深度学习\Datawhale\2023-8\Django4.2\My_Django

(django) D:\常用文件\CQU\就业\深度学习\Datawhale\2023-8\Django4.2\My_Django>django-admin startproject My_Django

(django) D:\常用文件\CQU\就业\深度学习\Datawhale\2023-8\Django4.2\My_Django>

After running django-admin startproject My_Django, there is an extra folder under My_Django , and there is manage.py under My_Djangothe extra My_Djangofolder , indicating that the operation was successful! My_Django! !
Please add image description

5. Create app

First switch the path to the extra My_Django folder, cd My_Django
and then continue to run in the window: python manage.py startapp myApp
At this time, an extra myApp folder will appear, indicating that the operation is successful! ! !
Please add image description

Note: Do not include Chinese characters in the path to manage.py, as coding errors may occur.

6. Set the setting.py file

Find setting.py in the My_Django folder
Please add image description

Open and add your own app name to INSTALLED_APPS and add the newly installed library:

    'rest_framework',
    'django_filters',
    'drf_spectacular',

Please add image description

7. Start the project

运行项目先执行数据库相关操作,再启动 django 项目
 ```bash
    # 先执行数据库迁移相关操作
    python manage.py makemigrations
    python manage.py migrate
    # 启动 django 项目
    python manage.py runserver
    ```
启动之后,复制如下链接去浏览器打开

Please add image description

看到下面这样的界面,就说明成功启动了!!!

Please add image description

8. Django-Build data table example (merge database)

In the myApp folder, find the model.py file
Please add image description

Open and add content:

## 产品分类表
class GoodsCategory(Model):
    """产品分类"""

    name = CharField(max_length=64,verbose_name='分类名称')
    remark = CharField(max_length=64,null=True,verbose_name='备注',blank=True)


## 产品表
class Goods(Model):
    """产品"""
    
    # 外键
    category = ForeignKey(GoodsCategory, on_delete=SET_NULL,related_name='goods_set',null=True,verbose_name='产品分类',blank=True,)
    # on_delete 


    number = CharField(max_length=32,verbose_name='产品编号')
    name = CharField(max_length=64,verbose_name='产品名称')
    barcode = CharField(max_length=32,null=True,blank=True,verbose_name='条码')
    spec = CharField(max_length=64,null=True,blank=True,verbose_name='规格')
    shelf_life_days = IntegerField(null=True, verbose_name='保质期天数')
    purchase_price = FloatField(default=0, verbose_name='采购价')
    retail_price = FloatField(default=0, verbose_name='零售价')
    remark = CharField(max_length=128, null=True, blank=True, verbose_name='备注')



"""
CharField:用于存储字符串类型,有最大长度限制
IntegerField:用于存储整数类型
FloatField:用于存储浮点数类型
BooleanField:用于存储布尔类型
DateField:用于存储日期类型
DateTimeField:用于存储日期和时间类型
ImageField:用于存储图片类型
FileField:用于存储文件类型
ForeignKey:外键 用于表示数据库表之间的关联关系
OneToOneField:一对一 用于表示一对一的关联关系
ManyToManyField:多对多 用于表示多对多的关联关系


max_length:字段的最大长度限制,可以应用于多种不同的字段类型。
verbose_name:字段的友好名称,便于在管理员后台可视化操作时使用。
default:指定字段的默认值。
null:指定字段是否可以为空。null=True 设置允许该字段为 NULL 值
blank:指定在表单中输入时是否可以为空白。
choices:用于指定字段的可选值枚举列表。
related_name:指定在多对多等关系中反向使用的名称。
on_delete:指定如果外键关联的对象被删除时应该采取什么操作。
"""

Then save and exit, and re-run the following command:
python manage.py makemigrations
python manage.py migrate

These two commands are key commands in the Django framework and are used for database migration. After you modify a Django model, you need to run these two commands to apply the changes to the database.

  1. python manage.py makemigrations: This command is used to generate migration scripts. After you update the model file, you need to run this command. Django will detect the model changes, then automatically generate the corresponding migration script and store it in the migrations/directory. Generally speaking, you need to run this command once per application.
  2. python manage.py migrate: This command is used to apply migration scripts to the database. After you make changes in the model file, you need to makemigrationsgenerate migration scripts through the command, and then run the command to apply these scripts to the database. For new migration scripts, Django executes them one by one, updating the database structure. For scripts that have already been executed, Django will skip them to avoid repeated execution.

These two commands are very important commands in the Django framework. You must always remember to use them clearly when modifying database-related content.

Restart the django project
python manage.py runserver

Guess you like

Origin blog.csdn.net/qq_42859625/article/details/132309863