django Post Form对象没有属性“cleaned”

cgvd09ve  于 2023-05-01  发布在  Go
关注(0)|答案(2)|浏览(101)

这是我的申请表

from django import forms

from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'
    
    def clean_slug(self):
        return self.cleaned['slug'].lower()

以下是我的Traceback:

Traceback (most recent call last):
  File "django\core\handlers\exception.py", line 
56, in inner
    response = get_response(request)
  File "django\core\handlers\base.py", line 197, 
in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django\views\generic\base.py", line 103, 
in view
    return self.dispatch(request, *args, **kwargs)
  File "django\views\generic\base.py", line 142, 
in dispatch
    return handler(request, *args, **kwargs)
  File "E:\Django Unleashed\suorganizer\blog\views.py", line 28, in post
    if bound_form.is_valid():
  File "django\forms\forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "django\forms\forms.py", line 452, in _clean_fields
    value = getattr(self, "clean_%s" % name)()
  File "suorganizer\blog\forms.py", line 11, in clean_slug
    return self.cleaned['slug'].lower()
AttributeError: 'PostForm' object has no attribute 'cleaned'
[26/Apr/2023 14:51:24] "POST /blog/create/ HTTP/1.1" 500 93820
iqih9akk

iqih9akk1#

变更:

return self.cleaned['slug'].lower()

对此:

return self.cleaned_data['slug'].lower()
gpfsuwkq

gpfsuwkq2#

试试这个,我想它会解决你的问题:

return self.cleaned_data.get('slug', '').lower()

相关问题