Django错误“NoneType”对象没有属性“\u meta”

2024-10-08 18:22:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图建立一个社区网页,既有一个新的张贴和所有以前的帖子列表,类似于facebook的形式。但现在我不断地犯错误

'NoneType' object has no attribute '_meta'

这是我的视图函数:

def community(request):
    if redirect_if_false_choice(request):
        return redirect_if_false_choice(request)
    posts = CommunityPost.objects.filter(public=True).order_by('date_posted')
    form = PostForm(request.POST or None)
    if str(request.method) == 'GET':
       return render(request, 'officer/community.html', {'posts': posts, 'form': form})
    if form.is_valid():
       form.save()
       return redirect('community')

这是表格:

class PostForm(forms.ModelForm):
   class Meta:
        model = CommunityPost
        fields = ('content', )
        widgets = {
            'content': forms.Textarea(attrs={'rows': 4, 'placeholder': 
            "What's going on?"})
        }

整个堆栈跟踪:

Internal Server Error: /officer/community/
Traceback (most recent call last):
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/security_officer_wanted/officer/views.py", line 114, in community
    return render(request, 'officer/community.html', {'posts': posts, 'form': form})
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/django/template/library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/home/christian/PycharmProjects/django-securityofficerwanted/venv/lib/python3.6/site-packages/image_cropping/templatetags/cropping.py", line 18, in cropped_thumbnail
    ratiofield = instance._meta.get_field(ratiofieldname)
AttributeError: 'NoneType' object has no attribute '_meta'
[04/Jul/2019 06:18:57] "GET /officer/community/ HTTP/1.1" 500 169040

Tags: djangoinpyhomevenvlibpackagesline
2条回答

我想出来了,错误是网站不知道谁发了一个帖子,我修正了,现在它工作了。 (模板调用CommunityPost模型中的函数,该函数应返回发布帖子的用户。但由于没有人发布,所以没有人回复。)

从你提供的信息无法确定。你知道吗

报告给你的错误,毫无疑问,提到了一个行号,这将是非常有益的,看看它,你几乎可以肯定是什么意思。产生错误的代码行很可能在Django库中,但是您可以在任何合理的调试器中跟踪堆栈,直到代码调用Django并在那里挠头。你知道吗

也就是说,一个明显的问题是,上面的元类不是PostForm类的成员。请参见:

https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/

举个例子:

class PostForm(forms.ModelForm):
    class Meta:
        model = CommunityPost
        fields = ('content', )
        widgets = {
            'content': forms.Textarea(attrs={'rows': 4, 'placeholder': "What's going on?"})
        }

但我突然想到,这很可能是你在这里发布的一个格式错误。很不确定。我只能说,这看起来不对。你知道吗

相关问题 更多 >

    热门问题