django.core.exceptions.ImproperlyConfigured:禁止创建没有 'fields' 属性或 'exclude' 属性的 ModelForm

问题描述 投票:0回答:8
(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 速成课程:基于项目的实践编程入门》这本书来学习 Python。当我“运行服务器”时,它只显示上述错误。我不知道出了什么问题,因为我完全按照书上说的做了。有人可以帮我吗? 当我尝试上述步骤时发生了什么。

有什么想法吗?

python django
8个回答
27
投票

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

pub_date、标题、内容、报告者这些是模型中的字段。 如果您选择包含所有字段,请像这样。

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,而无需使用使用字段或排除的两个选项之一。


6
投票


3
投票


2
投票
检查姓名
fields

,您输入错误,或者使用了大写F。

    


0
投票


0
投票


0
投票


-1
投票
field

而不是

fields
,所以首先检查名称字段,您可能输入错误。
    

© www.soinside.com 2019 - 2024. All rights reserved.