创建帐户

时间:2018-03-19 10:23:23

标签: python django

我有一个我在申请中使用的教师帐户。现在的问题是,我修改了一些东西,我无法弄清楚错误,因为当我尝试创建一个帐户时,我得到了这个错误:

IntegrityError at /admin/login/
(1062, "Duplicate entry '' for key 'email'")
HEre是追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/login/?next=/admin/

Django Version: 2.0.1
Python Version: 3.6.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'courses']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in get_or_create
  487.             return self.get(**lookup), False

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in get
  403.                 self.model._meta.object_name

During handling of the above exception (Student matching query does not exist.), another exception occurred:

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\mysql\base.py" in execute
  71.             return self.cursor.execute(query, args)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in execute
  250.             self.errorhandler(self, exc, value)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\connections.py" in defaulterrorhandler
  50.         raise errorvalue

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in execute
  247.             res = self._query(query)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in _query
  411.         rowcount = self._do_query(q)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in _do_query
  374.         db.query(q)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\connections.py" in query
  292.             _mysql.connection.query(self, query)

The above exception ((1062, "Duplicate entry '' for key 'email'")) was the direct cause of the following exception:

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\template\base.py" in render
  173.                 with context.bind_template(self):

File "c:\users\danny\appdata\local\programs\python\python36-32\Lib\contextlib.py" in __enter__
  81.             return next(self.gen)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\template\context.py" in bind_template
  250.             updates.update(processor(self.request))

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\eve\courses\context_processors.py" in notifications
  5.     if request.user.is_authenticated and request.user.student:

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\utils\functional.py" in inner
  216.         return func(self._wrapped, *args)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\eve\courses\models.py" in <lambda>
  185. User.student = property(lambda p: Student.objects.get_or_create(user=p)[0])

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in get_or_create
  489.             return self._create_object_from_params(lookup, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in _create_object_from_params
  528.             raise e

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in _create_object_from_params
  521.                 obj = self.create(**params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in create
  417.         obj.save(force_insert=True, using=self.db)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\base.py" in save
  729.                        force_update=force_update, update_fields=update_fields)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\base.py" in save_base
  759.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\base.py" in _save_table
  842.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\base.py" in _do_insert
  880.                                using=using, raw=raw)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\query.py" in _insert
  1125.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1281.                 cursor.execute(sql, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in execute
  100.             return super().execute(sql, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\django\db\backends\mysql\base.py" in execute
  71.             return self.cursor.execute(query, args)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in execute
  250.             self.errorhandler(self, exc, value)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\connections.py" in defaulterrorhandler
  50.         raise errorvalue

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in execute
  247.             res = self._query(query)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in _query
  411.         rowcount = self._do_query(q)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\cursors.py" in _do_query
  374.         db.query(q)

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\lib\site-packages\MySQLdb\connections.py" in query
  292.             _mysql.connection.query(self, query)

Exception Type: IntegrityError at /admin/login/
Exception Value: (1062, "Duplicate entry '' for key 'email'")

似乎错误来自于此:

from courses.models import Notification


def notifications(request):
    if request.user.is_authenticated and request.user.student:
        student_notifications = Notification.objects.filter(course__student__student_ID=request.user.student.student_ID)
        return {
            'notifications': student_notifications
        }
    return {}

1 个答案:

答案 0 :(得分:5)

我会检查这一行。根据你的追踪,IntegrityError来自那里:

File "C:\Users\danny\Desktop\Projects\bachelor_thesis\eve\courses\models.py" in <lambda>
185. User.student = property(lambda p: Student.objects.get_or_create(user=p)[0])

它说"Duplicate entry '' for key 'email'" 第一个字符串''是数据库中的现有条目。

看起来像:

  • 您的email字段设置为unique=True(可能blank=True
  • 您已经有一个Student对象,其中包含空电子邮件(''
  • 您正在尝试使用空电子邮件添加新的