gpt4 book ai didi

python - django.core.exceptions.ImproperlyConfigured : Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited

转载 作者:行者123 更新时间:2023-11-28 19:52:42 26 4
gpt4 key购买 nike

(ll_env) brads-MacBook-Pro:learning_log $ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d417d08>
Traceback (most recent call last):
File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/learning_log/learning_log/urls.py", line 23, in <module>
url(r'',include('learning_logs.urls',namespace='learning_logs')),
File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/learning_log/learning_logs/urls.py", line 2, in <module>
from . import views
File "/learning_log/learning_logs/views.py", line 6, in <module>
from .forms import TopicForm
File "/learning_log/learning_logs/forms.py", line 4, in <module>
class TopicForm(forms.ModelForm):
File "/learning_log/ll_env/lib/python3.6/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 TopicForm needs updating.





部分代码:

管理.py

#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'learning_log.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)

views.py

from django.shortcuts import render
from .models import Topic
from django.http import HttpResponseRedirect
#from django.core.urlresolvers import reverse
from django.urls import reverse
from .forms import TopicForm

# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')

def topics(request):
topics=Topic.objects.order_by('date_added')
context={'topics':topics}
return render(request,'learning_logs/topics.html',context)

def topic(request,topic_id):
topic=Topic.objects.get(id=topic_id)
entries=topic.entry_set.order_by('-date_added')
context={'topic':topic,'entries':entries}
return render(request,'learning_logs/topic.html',context)

def new_topic(request):
if request.method != 'POST':
form = TopicForm()
else:
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('learning_logs:topics'))

context = {'form',form}
return render(request,'learning_logs/new_topic.html',context)

因此,我一直在使用 Python 速成类(class):基于项目的动手编程入门 来学习 Python。当我“运行服务器”时,它只显示上述错误。我不知道哪里出了问题,因为我完全按照书上说的做了。谁能帮帮我?

当我尝试上述步骤时发生了什么。

有什么想法吗?

最佳答案

当您使用模型创建表单时,您需要指定要包含在表单中的字段。

例如,您有一个名为 Article 的模型,您想要为模型文章创建一个表单。

pub_date, headline, content, reporter 这些是模型中的字段。如果您选择包括所有字段,您可以这样做。

class ArticleForm(ModelForm):
class Meta:
model = Article
fields = '__all__'

如果要指定要包含哪些字段

class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content']

如果你想排除某些字段并使用剩余的那么使用如下

class ArticleForm(ModelForm):
class Meta:
model = Article
exclude = ['headline']

针对您的错误,它说您可以使用 ModelForm 而无需使用使用字段或排除的两个选项之一。

关于python - django.core.exceptions.ImproperlyConfigured : Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51734479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com