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

时间:2018-08-07 19:53:27

标签: python django

    (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.





**some part of the code:
manage.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。当我“运行”时,它只是显示了上面的错误。我不知道哪里出了问题,因为我完全按照书中所说的去做。有人可以帮我吗?

尝试上述步骤时发生了什么情况。

有什么想法吗?

6 个答案:

答案 0 :(得分:1)

class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic
        fields = '__all__' # or whatever fields you want ('field_a', )

答案 1 :(得分:1)

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

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

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

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

如果要指定要包括的字段

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

如果您要排除某些字段并使用其余字段,请按如下操作

{{1}}

出现错误时,它说您可以在不使用两个使用字段或排除的选项之一的情况下使用ModelForm。

答案 2 :(得分:1)

我有完全相同的错误,结果我写的是“字段”而不是“字段” '

答案 3 :(得分:0)

class BlogForm(forms.ModelForm):
    class Meta:
        model=Blog
        fields='__all__'

检查名称fields,输入错误或使用大写字母F。

答案 4 :(得分:0)

from django import forms
from .models import Dforms

class Forms(forms.ModelForm):
    class Meta:
        model = Dforms
        fields = "__all__"

答案 5 :(得分:0)

我遇到了同样的问题,我的错误是在我的应用的 forms.py 文件中遗漏了一个类的名称