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

相关推荐
小柯南敲键盘几秒前
图片翻译API接入与自动化实现指南
运维·python·自动化
旅僧24 分钟前
Q-learning(自用)
python·机器学习
Python大数据分析@44 分钟前
曝梁文锋称「一度不想维护C端用户,奈何赶不走」,DeepSeek真不需要C端用户吗?可能会有啥影响吗?
python
艾斯特_2 小时前
工作流与多Agent协作:LangGraph、MCP和A2A的应用分层
人工智能·python·ai
IT小盘2 小时前
04-大模型流式输出原理-SSE与Python实现
开发语言·网络·人工智能·python
我叫黑大帅2 小时前
add()和 __add__() 写法哪个更好呢?
后端·python·面试
不如语冰2 小时前
AI大模型入门-Python进阶-上下文管理与with语句
开发语言·数据结构·数据库·人工智能·pytorch·redis·python
柠檬味的Cat2 小时前
GEO优化系统哪个渠道商好
大数据·人工智能·python
青春不败 177-3266-05203 小时前
基于Python实现的深度学习技术在水文水质领域应用
python·深度学习·机器学习·水文水资源·水质模型
阿童木写作3 小时前
Python批量翻译亚马逊商品图实战教程
开发语言·python·xcode