博客五部曲之一 - 简单博客


1471 浏览 5 years, 11 months

5 超级用户和管理

版权声明: 转载请注明出处 http://www.codingsoho.com/

migrate数据库

执行migrate命令生成数据库表

python manager.py migrate

(env) E:\Computer\virtualenv\iblog\csblog>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... 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 auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

创建超级用户

python manager.py createsuperuser

(env) E:\Computer\virtualenv\iblog\csblog>python manage.py createsuperuser
Username (leave blank to use 'administrator'): bhe001
Email address:
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is entirely numeric.
Password:
Password (again):
Superuser created successfully.

邮箱地址可选。密码在该版本有一些限制,至少为8个字符。

# Database
# [https://docs.djangoproject.com/en/1.11/ref/settings/](https://docs.djangoproject.com/en/1.11/ref/settings/)#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

配置

查看settings.py,默认的数据库位sqlite,这可以满足我们一般的练习需求,如果产品化的时候,我们可以迁移到MySQL,Oracle等数据库上。

在创建完超级用户之后,我们接下来可以访问http://127.0.0.1:8000/admin,进行后台管理,默认有权限相关的配置,这些在默认的INSTALLED_APPS里面已定义。

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我们也可以直接在后台添加新的用户。

其他的一些配置的介绍

ROOT_URLCONF,这是根URL的配置,指示URL定义位置

ROOT_URLCONF = 'csblog.urls'

除了上面提到的这些命令,django manager还提供了一些其他的命令,参考下面

(env) E:\Computer\virtualenv\iblog\csblog>python manage.py

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[contenttypes]
    remove_stale_contenttypes

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver