Admin后台管理-django

时间:2021-02-14 19:16:22

Admin后台管理-django

@(python)

激活管理界面

  • 首先将django.contrib.admin加入setting.py的INSTALLED_APPS配置中;
    保证INSTALLED_APPS中包含django.contrib.authdjango.contrib.contenttypesdjango.contrib.sessions
    MIDDLEWARE_CLASSES包含django.middleware.common.CommonMiddlewaredjango.contrib.sessions.middleware.SessionMiddlewaredjango.contrib.auth.middleware.AuthenticationMiddleware
  • 执行python manage.py migrate
    输出是

    Operations to perform:
      Apply all migrations: admin, contenttypes, users, auth, sessions
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying sessions.0001_initial... OK
  • 运行python manage.py createsuperuser创建管理员账号。
    输出是

    linaro@cubietruck:~/temp/mysite$ python manage.py createsuperuser
    Username (leave blank to use 'linaro'): admin
    Email address: admin@qq.com
    Password: 
    Password (again): 
    Superuser created successfully.
    linaro@cubietruck:~/temp/mysite$ 
  • 在浏览器中访问http://ip:8000/admin/访问管理器
    Admin后台管理-django
    Admin后台管理-django

修改模型数据

  • 使用django自带的web管理界面admin来在页面上修改模型数据。
  • 模型注册,需要在admin中注册对应的模型,打开users/admin.py文件,修改如下

    from django.contrib import admin
    from users.models import Info
    # Register your models here.
    admin.site.register(Info)

    此时刷新管理器页面,可以看见新增的users项
    Admin后台管理-django
    Admin后台管理-django

参考文献

http://raytaylorlin.com/Tech/Script/Python/django-note-4/
http://www.pycoding.com/articles/135.html