我正在尝试构建一个社区页面,该页面既具有用于发布新帖子的形式,又具有所有先前帖子的列表,类似于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
答案 0 :(得分:0)
从您提供的信息中无法确定。
毫无疑问,报告给您的错误提到了一个行号,该行号非常有用,看着它,您几乎可以肯定这是什么意思。产生错误的行可能在Django库中,但是您可以在任何明智的调试器中跟踪堆栈,直至代码在Django上调用并在那里抓头。
那是可能与您的经历有关的一个明显问题,即您上面的Meta类不是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?"})
}
但是令我惊讶的是,这很可能是您在此处发布信息时出现的格式错误。非常不确定。我只能说,它看起来是错误的。
答案 1 :(得分:0)
我知道了,错误是该网站不知道是谁张贴了帖子,我已将其修复,现在可以正常工作了。
(模板在CommunityPost
模型中调用了一个函数,该函数应返回发布该帖子的用户。但是由于没有人发布,因此它不返回任何帖子。)