新建app
命令:startapp users
第一步:
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(choices=(max_length=6, ("male",u"男"),("female","女")), default="female")
address = models.CharField(max_length=100, default=u"")
mobile = models.CharField(max_length=11, null=True, blank=True)
image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.jpg")
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username
gender = models.CharField(max_length = 8, choices=(("male", u"男"), ("female", "女")), default = "female")
第二步:
在setting注册新建的app
AUTH_USER_MODEL = "users.UserProfile"
报错:
ERRORS:
users.UserProfile.gender: (fields.E120) CharFields must define a 'max_length' attribute.
users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".
System check identified 2 issues (0 silenced).
ERRORS:
users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".
安装:pip install Pillow
重新运行项目
pydev debugger: process 7728 is connecting
Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace instance at 0x029A8EE0>
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 589, in __call__
return self.original_func(*self.args, **self.kwargs)
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check_migrations()
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\core\management\commands\runserver.py", line 163, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 299, in build_graph
parent = self.check_key(parent, key[0])
File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 160, in check_key
raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: users
解决办法:
输入命令:
生成数据表:
migrate users
输入:yes
manage.py@fenghua > makemigrations users
"C:\Program Files\JetBrains\PyCharm 2017.2.7\bin\runnerw.exe" D:\Envs\zhanglijie\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2017.2.7\helpers\pycharm\django_manage.py" makemigrations users C:/Users/huang/PycharmProjects/fenghua
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.2.7\helpers\pycharm\django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/huang/PycharmProjects/fenghua\manage.py", line 21, in <module>
main()
File "C:/Users/huang/PycharmProjects/fenghua\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\commands\makemigrations.py", line 92, in handle
loader.check_consistent_history(connection)
File "D:\Envs\zhanglijie\lib\site-packages\django\db\migrations\loader.py", line 291, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
Process finished with exit code 1
manage.py@fenghua > migrate users
"C:\Program Files\JetBrains\PyCharm 2017.2.7\bin\runnerw.exe" D:\Envs\zhanglijie\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2017.2.7\helpers\pycharm\django_manage.py" migrate users C:/Users/huang/PycharmProjects/fenghua
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.2.7\helpers\pycharm\django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "c:\users\huang\appdata\local\programs\python\python36\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/huang/PycharmProjects/fenghua\manage.py", line 21, in <module>
main()
File "C:/Users/huang/PycharmProjects/fenghua\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "D:\Envs\zhanglijie\lib\site-packages\django\core\management\commands\migrate.py", line 82, in handle
executor.loader.check_consistent_history(connection)
File "D:\Envs\zhanglijie\lib\site-packages\django\db\migrations\loader.py", line 291, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
Process finished with exit code 1
把所有的表 删除
依次运行:
makemigrations
migrate
还得重新创建一个账户
createsuperuser
"C:\Program Files\JetBrains\PyCharm 2017.2.7\bin\runnerw.exe" D:\Envs\GroupPurchase\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2017.2.7\helpers\pycharm\django_manage.py" createsuperuser C:/Users/huang/PycharmProjects/gp
Username: admin
Email address: admin@126.com
Warning: Password input may be echoed.
Password: admin123
Warning: Password input may be echoed.
Password (again): admin123
The password is too similar to the email address.
Warning: Password input may be echoed.
Password: admin123.
Warning: Password input may be echoed.
Password (again): admin123.
The password is too similar to the email address.
Warning: Password input may be echoed.
Password: