ValueError: dictionary update sequence element #0 has length 8; 2 is required

时间:2022-07-07 02:25:29

在写django ModelForm时遇到这了该错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm\helpers\pycharm\django_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "C:\Program Files\Python35\lib\runpy.py", line 196, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Program Files\Python35\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:/MyBlog\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\myblogenv\lib\site-packages\django\core\management\__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "D:\myblogenv\lib\site-packages\django\core\management\__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\myblogenv\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\myblogenv\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "D:\myblogenv\lib\site-packages\django\core\management\commands\check.py", line 51, in handle
    include_deployment_checks=include_deployment_checks,
  File "D:\myblogenv\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\myblogenv\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\myblogenv\lib\site-packages\django\core\checks\urls.py", line 10, in check_url_config
    return check_resolver(resolver)
  File "D:\myblogenv\lib\site-packages\django\core\checks\urls.py", line 19, in check_resolver
    for pattern in resolver.url_patterns:
  File "D:\myblogenv\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\myblogenv\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\myblogenv\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\myblogenv\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Program Files\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "D:/MyBlog\MyBlog\urls.py", line 27, in <module>
    url(r'^cuser/', include('cuser.urls')),
  File "D:\myblogenv\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Program Files\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "D:/MyBlog\cuser\urls.py", line 7, in <module>
    from .views import loginView, registerView, active_accountView, forgetpwdView, resetpwdView, uploadImageView,userInfoView
  File "D:/MyBlog\cuser\views.py", line 6, in <module>
    from .forms import LoginForm, RegisterForm, ForgetPwdForm, ResetPwdForm, UploadImageForm
  File "D:/MyBlog\cuser\forms.py", line 12, in <module>
    class LoginForm(ModelForm):
  File "D:\myblogenv\lib\site-packages\django\forms\models.py", line 247, in __new__
    opts.field_classes)
  File "D:\myblogenv\lib\site-packages\django\forms\models.py", line 172, in fields_for_model
    formfield = f.formfield(**kwargs)
  File "D:\myblogenv\lib\site-packages\django\db\models\fields\__init__.py", line 1120, in formfield
    return super(CharField, self).formfield(**defaults)
  File "D:\myblogenv\lib\site-packages\django\db\models\fields\__init__.py", line 903, in formfield
    return form_class(**defaults)
  File "D:\myblogenv\lib\site-packages\django\forms\fields.py", line 228, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "D:\myblogenv\lib\site-packages\django\forms\fields.py", line 129, in __init__
    messages.update(error_messages or {})

  经查找后发现:

class LoginForm(ModelForm):
    class Meta:
        model = UserProfiles
        fields = ['username', 'password']
        error_messages = {
            'username': {'required', 'username Required'},
            'password': {'required': 'Password contains digit and word.'}
        }

  上面的LoginForm在定义时,error_messages 字段对应的错误提示应该是字典,但第一个username的字典中间误写成了逗号,导致了该错误

将其修改后为冒号后,一切正常.