python-Django中连接MySQL数据库及设置用户名密码

项目和应用创建好以后,进入当前的目录所在的文件夹即可操作,也可以用pycharm中的Tools工具运行manage.py,本人采用的是运行pycharm下的manage.py文件
配置:
1.把对应的应用添加到INSTALLED_APPS下,注意分号
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',


    'blog', #应用
]


2.修改数据库连接
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # 提示连接mysql数据库
        'NAME': 'test', # 数据库名为test,要自己创建
        'USER': 'root', # 用户名
        'PASSWORD': 'root',	# 密码
        'HOST': '127.0.0.1', # 连接的主机
        'PORT': '3306', # 对应的端口号
    }
}


运行:
manage.py@hello_mysite > migrate # 连接数据库
"D:\pycharm\PyCharm 5.0.3\bin\runnerw.exe" D:\python\python.exe "D:\pycharm\PyCharm 5.0.3\helpers\pycharm\django_manage.py" migrate G:/hello_mysite
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


Process finished with exit code 0
manage.py@hello_mysite > makemigrations blog # 关联应用
"D:\pycharm\PyCharm 5.0.3\bin\runnerw.exe" D:\python\python.exe "D:\pycharm\PyCharm 5.0.3\helpers\pycharm\django_manage.py" makemigrations blog G:/hello_mysite
No changes detected in app 'blog'


Process finished with exit code 0
manage.py@hello_mysite > createsuperuser # 创建超级用户
"D:\pycharm\PyCharm 5.0.3\bin\runnerw.exe" D:\python\python.exe "D:\pycharm\PyCharm 5.0.3\helpers\pycharm\django_manage.py" createsuperuser G:/hello_mysite
Username:  root # 用户名
Email address:  [email protected] # 邮箱
Warning: Password input may be echoed.
Password:  root1234 # 密码,不要太短
Warning: Password input may be echoed.
Password (again):  root1234 # 再次输入
Superuser created successfully.


到此,一次OK了!!

你可能感兴趣的:(python-Django中连接MySQL数据库及设置用户名密码)