python-0004-django站点

站点

django自带站点用来操作增删改查

国际化(汉化)

项目setting.py

python 复制代码
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True

创建模型

在子应用的models.py中创建,如下:

python 复制代码
from django.db import models

# Create your models here.
from django.db import models


class BookInfo(models.Model):
    name = models.CharField(max_length=15)

    def __str__(self):
        return self.name
    pass


class PeopleInfo(models.Model):
    name = models.CharField(max_length=15)
    gender = models.BooleanField()
    book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)
    pass

生成迁移文件

在pycharm的终端或虚拟环境执行

bash 复制代码
python manage.py makemigrations

执行迁移

在pycharm的终端或虚拟环境执行

bash 复制代码
python manage.py migrate

此时会在项目的db.sqlite3中生成信息

注册模型到站点

在子应用的admin.py中注册,示例如下:

python 复制代码
from django.contrib import admin

# Register your models here.
from book.models import BookInfo,PeopleInfo

admin.site.register(BookInfo)
admin.site.register(PeopleInfo)

重写__str__

目的:显示更全的数据

重写model的__str__,如:

python 复制代码
from django.db import models

# Create your models here.
from django.db import models


class BookInfo(models.Model):
    name = models.CharField(max_length=15)

    def __str__(self):
        return self.name
    pass


class PeopleInfo(models.Model):
    name = models.CharField(max_length=15)
    gender = models.BooleanField()
    book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)
    pass

创建超管

bash 复制代码
python manage.py createsuperuser

界面访问

http://ip:port/admin

相关推荐
2401_8288906420 分钟前
使用 BERT 实现意图理解和实体识别
人工智能·python·自然语言处理·bert·transformer
多恩Stone2 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
xiaopengbc2 小时前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@2 小时前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.2 小时前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置
yddddddy2 小时前
SQLite的基本操作
数据库·sqlite
偷心伊普西隆2 小时前
Python EXCEL 理论探究:格式转换时处理缺失值方法
python·excel
精灵vector3 小时前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain