zoukankan      html  css  js  c++  java
  • django后台管理

    后台管理

    1)  本地化

    语言和时区的本地化。

    修改settings.py文件。

    # LANGUAGE_CODE = 'en-us'
    LANGUAGE_CODE = 'zh-hans'
    
    # TIME_ZONE = 'UTC'
    TIME_ZONE = 'Asia/Shanghai' 

    2)  创建管理员

    命令:python manage.py createsuperuser

    根据指令往下填写 如果user为空 则默认是终端使用的user名称

    3)  注册模型类

    在应用下的admin.py中注册模型类。

    1 from django.contrib import admin
    2 from booktest.models import BookInfo, HeroInfo
    3 # 后台管理相关文件
    4 # Register your models here.
    5 # 注册模型类
    6 admin.site.register(BookInfo)
    7 admin.site.register(HeroInfo)

    告诉djang框架根据注册的模型类来生成对应表管理页面。如果想在后台看到图书名或这英雄名 要重写__str__方法

    如:

     1 from django.db import models
     2 
     3 # Create your models here.
     4 
     5 # 图书类
     6 class BookInfo(models.Model):
     7     """图书模型类"""
     8     # 图书名称 CharField是字符串类型,max_length制定字符串最大长度
     9     btitle = models.CharField(max_length=20)
    10     # 出版日期,DateField是一个日期类型
    11     bpub_date = models.DateField()
    12     
    13     def __str__(self):
    14         # 如果想在admin里看到图书名称 则要重写__str__方法
    15         return self.btitle

    b = BookInfo()

    str(b) __str__

    4)  自定义管理页面

    自定义模型管理类。模型管理类就是告诉django在生成的管理页面上显示哪些内容。

    自定义模型管理类在admin.py:

     1 from django.contrib import admin
     2 from booktest.models import BookInfo, HeroInfo
     3 # 后台管理相关文件
     4 # Register your models here.
     5 # 自定义管理类
     6 class BookInfoAdmin(admin.ModelAdmin):
     7     """图书模型管理类"""
     8     # list_display定义类属性
     9     list_display = ['id', 'btitle', 'bpub_date']
    10     
    11 class HeroInfoAdmin(admin.ModelAdmin):
    12     """英雄模型管理类"""
    13     list_display = ['id', 'hname', 'hcomment']
    14 # 注册模型类
    15 admin.site.register(BookInfo, BookInfoAdmin)
    16 admin.site.register(HeroInfo, HeroInfoAdmin)
  • 相关阅读:
    Balanced Binary Tree
    Swap Nodes in Pairs
    Reverse Nodes in k-Group
    Reverse Linked List II
    Remove Nth Node From End of List
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Partition List
    Merge Two Sorted Lists
    【Yii2.0】1.2 Apache检查配置文件语法
  • 原文地址:https://www.cnblogs.com/yifengs/p/11512143.html
Copyright © 2011-2022 走看看