我正在执行django project 2.1.4,并得到以下错误,我进行了搜索,似乎我一个一个地修复了错误,但是我设法解决了一些问题。 但是这些我都能找到任何线索。
(myenv) C:\Users\Users\Desktop\blog_project\mysite>python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001EC9271A0D0>
Traceback (most recent call last):
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\utils\autoreload.py", line 225,
in wrapper
fn(*args, **kwargs)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\management\commands\runserv
er.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\management\base.py", line 3
79, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\management\base.py", line 3
66, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\checks\registry.py", line 7
1, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\checks\urls.py", line 13, i
n check_url_config
return check_resolver(resolver)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\core\checks\urls.py", line 23, i
n check_resolver
return check_method()
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\urls\resolvers.py", line 396, in
check
for pattern in self.url_patterns:
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\utils\functional.py", line 37, i
n __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\urls\resolvers.py", line 533, in
url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\utils\functional.py", line 37, i
n __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\urls\resolvers.py", line 526, in
urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Users\Desktop\blog_project\mysite\mysite\urls.py", line 22, in <module>
path('', include('blog.urls')),
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\urls\conf.py", line 34, in inclu
de
urlconf_module = import_module(urlconf_module)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Users\Desktop\blog_project\mysite\blog\urls.py", line 2, in <module>
from .import views
File "C:\Users\Users\Desktop\blog_project\mysite\blog\views.py", line 4, in <module>
from blog.forms import PostForm,CommentForm
File "C:\Users\Users\Desktop\blog_project\mysite\blog\forms.py", line 15, in <module>
class CommentForm(forms.ModelForm):
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\forms\models.py", line 243, in _
_new__
"needs updating." % name
django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute
or the 'exclude' attribute is prohibited; form CommentForm needs updating.
urlconf_module = import_module(urlconf_module)
File "C:\Users\Users\Anaconda3\envs\myenv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Users\Desktop\blog_project\mysite\blog\urls.py", line 2, in <module>
from .import views
File "C:\Users\Users\Desktop\blog_project\mysite\blog\views.py", line 4, in <module>
from blog.forms import PostForm,CommentForm
File "C:\Users\Users\Desktop\blog_project\mysite\blog\forms.py", line 15, in <module>
class CommentForm(forms.ModelForm):
File "C:\Users\Users\Anaconda3\envs\myenv\lib\site-packages\django\forms\models.py", line 243, in _
_new__
"needs updating." % name
django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute
or the 'exclude' attribute is prohibited; form CommentForm needs updating.
在这里,我只是单独添加。
File "C:\Users\Users\Desktop\blog_project\mysite\mysite\urls.py", line 22, in <module>
path('', include('blog.urls')),
'
path('', include('blog.urls')),
'
File "C:\Users\Users\Desktop\blog_project\mysite\blog\urls.py", line 2, in <module>
from .import views
'
from .import views
'
File "C:\Users\Users\Desktop\blog_project\mysite\blog\views.py", line 4, in <module>
from blog.forms import PostForm,CommentForm
'
from blog.forms import PostForm,CommentForm
'
File "C:\Users\Users\Desktop\blog_project\mysite\blog\forms.py", line 15, in <module>
class CommentForm(forms.ModelForm):
'
class CommentForm(forms.ModelForm):
'
如何解决此错误?
更新:下面的评论表格
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fileds = ('author','text',)
widgets = {
'author':forms.TextInput(attrs={'class':'textinputclass'}),
'text':forms.Textarea(attrs={'class':'editable medium-editor-textarea'})
}
答案 0 :(得分:2)
从您的CommentForm
可以看出,您将fields
拼写为fileds
。更正该错误以解决该问题:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('author','text',) # Use correct spelling
widgets = {
'author':forms.TextInput(attrs={'class':'textinputclass'}),
'text':forms.Textarea(attrs={'class':'editable medium-editor-textarea'})
}
答案 1 :(得分:1)
您需要在模型表单中添加“字段”或“排除”属性。
# example model:
class Comments(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
comment = models.TextField()
# forms:
class CommentForm(forms.ModelForm):
class Meta(object):
model = Comments
exclude = ['first_name', ]
# or:
fields = ['comments', ]