站点
django自带站点用来操作增删改查
国际化(汉化)
项目setting.py
# 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中创建,如下:
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的终端或虚拟环境执行
python manage.py makemigrations
执行迁移
在pycharm的终端或虚拟环境执行
python manage.py migrate
此时会在项目的db.sqlite3中生成信息
注册模型到站点
在子应用的admin.py中注册,示例如下:
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__,如:文章来源:https://www.toymoban.com/news/detail-840024.html
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
创建超管
python manage.py createsuperuser
界面访问
http://ip:port/admin文章来源地址https://www.toymoban.com/news/detail-840024.html
到了这里,关于python-0004-django站点的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!