首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoneType错误'_meta‘对象没有属性’_meta‘

NoneType错误'_meta‘对象没有属性’_meta‘
EN

Stack Overflow用户
提问于 2019-07-04 06:23:28
回答 2查看 903关注 0票数 0

我正在尝试建立一个社区页面,它既包括发布新帖子的表单,也包括以前所有帖子的列表,类似于facebook。但现在我一直搞错了

代码语言:javascript
复制
'NoneType' object has no attribute '_meta'

这是我的观点功能:

代码语言:javascript
复制
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')

以下是表格:

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

整个堆栈跟踪:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-04 06:54:43

我想通了,错误是网站不知道是谁发的帖子,我把它修好了,现在它起作用了。(模板调用CommunityPost模型中的一个函数,该函数应该返回发布帖子的用户。但既然没有人发出去,它就一文不名。)

票数 0
EN

Stack Overflow用户

发布于 2019-07-04 06:40:19

从你提供的信息来看是不可能确定的。

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

也就是说,一个可能与您所经历的相关的明确问题是,上面的元类不是PostForm类的成员。请参见:

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

并尝试例如:

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

但我觉得这可能是你在这里的文章中的一个格式错误。很不确定。我只能说,看上去不对。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56881742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档