在写django ModelForm时遇到这了该错误:
Traceback (most recent call last): File "C:Program Files (x86)JetBrainsPyCharmhelperspycharmdjango_manage.py", line 41, in <module> run_module(manage_file, None, '__main__', True) File "C:Program FilesPython35lib unpy.py", line 196, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "C:Program FilesPython35lib unpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:Program FilesPython35lib unpy.py", line 85, in _run_code exec(code, run_globals) File "D:/MyBlogmanage.py", line 10, in <module> execute_from_command_line(sys.argv) File "D:myblogenvlibsite-packagesdjangocoremanagement\__init__.py", line 350, in execute_from_command_line utility.execute() File "D:myblogenvlibsite-packagesdjangocoremanagement\__init__.py", line 342, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:myblogenvlibsite-packagesdjangocoremanagementase.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "D:myblogenvlibsite-packagesdjangocoremanagementase.py", line 399, in execute output = self.handle(*args, **options) File "D:myblogenvlibsite-packagesdjangocoremanagementcommandscheck.py", line 51, in handle include_deployment_checks=include_deployment_checks, File "D:myblogenvlibsite-packagesdjangocoremanagementase.py", line 426, in check include_deployment_checks=include_deployment_checks, File "D:myblogenvlibsite-packagesdjangocorechecks egistry.py", line 75, in run_checks new_errors = check(app_configs=app_configs) File "D:myblogenvlibsite-packagesdjangocorechecksurls.py", line 10, in check_url_config return check_resolver(resolver) File "D:myblogenvlibsite-packagesdjangocorechecksurls.py", line 19, in check_resolver for pattern in resolver.url_patterns: File "D:myblogenvlibsite-packagesdjangoutilsfunctional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "D:myblogenvlibsite-packagesdjangocoreurlresolvers.py", line 417, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "D:myblogenvlibsite-packagesdjangoutilsfunctional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "D:myblogenvlibsite-packagesdjangocoreurlresolvers.py", line 410, in urlconf_module return import_module(self.urlconf_name) File "C:Program FilesPython35libimportlib\__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:/MyBlogMyBlogurls.py", line 27, in <module> url(r'^cuser/', include('cuser.urls')), File "D:myblogenvlibsite-packagesdjangoconfurls\__init__.py", line 52, in include urlconf_module = import_module(urlconf_module) File "C:Program FilesPython35libimportlib\__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:/MyBlogcuserurls.py", line 7, in <module> from .views import loginView, registerView, active_accountView, forgetpwdView, resetpwdView, uploadImageView,userInfoView File "D:/MyBlogcuserviews.py", line 6, in <module> from .forms import LoginForm, RegisterForm, ForgetPwdForm, ResetPwdForm, UploadImageForm File "D:/MyBlogcuserforms.py", line 12, in <module> class LoginForm(ModelForm): File "D:myblogenvlibsite-packagesdjangoformsmodels.py", line 247, in __new__ opts.field_classes) File "D:myblogenvlibsite-packagesdjangoformsmodels.py", line 172, in fields_for_model formfield = f.formfield(**kwargs) File "D:myblogenvlibsite-packagesdjangodbmodelsfields\__init__.py", line 1120, in formfield return super(CharField, self).formfield(**defaults) File "D:myblogenvlibsite-packagesdjangodbmodelsfields\__init__.py", line 903, in formfield return form_class(**defaults) File "D:myblogenvlibsite-packagesdjangoformsfields.py", line 228, in __init__ super(CharField, self).__init__(*args, **kwargs) File "D:myblogenvlibsite-packagesdjangoformsfields.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的字典中间误写成了逗号,导致了该错误
将其修改后为冒号后,一切正常.