django后台设置

Django 后台管理设置步骤

一、环境设置

  1. linux python3.5 django mysql

二、创建项目

  1. 终端输入 python manage.py createsuper

  2. 输入用户 wk

  3. 输入密码 **

  4. 邮箱 *@163.com

  5. 设置为中文

      settings中LANGUAGE_CODE= ‘zh-hans’

    设置时间,时区

      TIME_ZONE=’Asia/Shanghai’

    添加自己的数据模型

      在admin.py中注册

      admin.site.register(xxx)

  6. ~~~python
    from django.contrib import admin

    Register your models here.

    from App.models import User

    class Useradmin(admin.ModelAdmin):

    def gender(self):
    if self.ugender:
    return ‘男’
    else:
    return ‘女’

    list_display = [‘uname’,’upassword’,gender]
    gender.short_description = ‘性别’

    fieldsets = (

       ('基本信息',({'fields':('ugender',)})),
       ('描述信息',({'fields':('upassword',)})),
    

    )
    exclude = [‘uname’,]

    限制每当添加一个班时,至少要有3个学生,才能添加成功

    class StudentInfo(admin.TabularInline):

    model = Student

    extra = 3

    #

    class Gradeadmin(admin.ModelAdmin):

    inlines = StudentInfo

    #

    class Studentadmin(admin.ModelAdmin):

    pass

    #

    admin.site.register(Grade.Gradeadmin)

    admin.site.register(Student.Studentadmin)

    admin.site.register(User,Useradmin)

    #

    class Myadminsite(admin.AdminSite):

    site_header = ‘千峰页面管理’

    site_url = ‘login’

    #

    mysite=Myadminsite()

    mysite.register(User)

    admin.py

    ~~~

  7. ~~~python
    from django.db import models

    Create your models here.

    class User(models.Model):
    uname=models.CharField(max_length=20)
    upassword=models.IntegerField()
    isDelete=models.BooleanField(default=False)
    ugender=models.BooleanField(default=True)

    def str(self):
    return self.uname
    class Grade(models.Model):
    pass
    class Student(models.Model):
    pass

    modles.py

    ~~~

你可能感兴趣的:(django后台设置)