簡體   English   中英

Django錯誤'NoneType'對象沒有屬性'_meta'

[英]Django Error 'NoneType' object has no attribute '_meta'

我正在嘗試建立一個社區頁面,該頁面既具有張貼新帖子的形式,又具有所有先前帖子的列表,類似於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

從您提供的信息中無法確定。

毫無疑問,報告給您的錯誤提到了一個行號,該行號非常有用,看着它幾乎可以肯定是什么意思。 產生錯誤的行可能在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?"})
        }

但是令我驚訝的是,這很可能是您在此處發布帖子時遇到的格式錯誤。 非常不確定。 我只能說,它看起來不對。

我弄清楚了,錯誤是該網站不知道是誰張貼了帖子,我對此進行了修復,現在可以正常工作了。 (該模板在CommunityPost模型中調用了一個函數,該函數應返回發布帖子的用戶。但是由於沒有人發布,因此它不返回任何內容。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM