django - 错误 'NoneType' 对象没有属性 '__dict__'

标签 django python-3.x django-models django-forms django-views

我遇到了这个错误,它不允许我将信息保存在表单中。初始数据在表格中显示良好,但保存对我来说是一个挑战。希望有人能帮忙,我真的很困惑

class UserPostCreatView(CreateView):
   form_class = PostModelForm
   template_name = 'posts/post_form.html'
   success_url = "/profile/{user_slug}/wall"



def get_initial(self):
    # Get the initial dictionary from the superclass method
    initial = super(UserPostCreatView, self).get_initial()
    user_slug = self.kwargs.get('user_slug')
    user_content_type = ContentType.objects.get_for_model(authomodel.User)
    auth_user = get_object_or_404(authomodel.User, user_slug=user_slug)
    auth_user_id = auth_user.id
    # Copy the dictionary so we don't accidentally change a mutable dict
    initial = initial.copy()
    initial = {
    "content_type": user_content_type,
    "object_id" : auth_user_id,
     }
    return initial

def form_valid(self, form):
    return HttpResponseRedirect(self.get_success_url())





def get_form_kwargs(self):
    """
    Returns the keyword arguments for instantiating the form.
    """
    kwargs = {
        'initial': self.get_initial(),
    }

    if self.request.method in ('POST', 'PUT'):
        kwargs.update({
            'data': self.request.POST or None, 
            'files': self.request.FILES or None})
    return kwargs


def get_form_class(self):
    return self.form_class

Traceback:

File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\exception.py" in inner 41. response = get_response(request)

File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response 249. response = self._get_response(request)

File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs)

File "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs)

File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in post 217. return super(BaseCreateView, self).post(request, *args, **kwargs)

File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in post 183. return self.form_valid(form)

File "C:\Users\wahab\Desktop\site1\ostra\ostrakodecommerce\posts\views.py" in form_valid 207. return HttpResponseRedirect(self.get_success_url())

File "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" in get_success_url 148. url = self.success_url.format(**self.object.dict)

Exception Type: AttributeError at /profile/-.1/create Exception Value: 'NoneType' object has no attribute 'dict'

最佳答案

您已覆盖 form_valid 方法,但尚未执行该方法执行的任何默认操作,特别是保存对象。

你可以通过调用 super 方法来解决这个问题,但是没有意义;无论如何,重定向到成功 URL 就是该方法的作用。完全删除 form_valid 方法并调用现有定义。

关于django - 错误 'NoneType' 对象没有属性 '__dict__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45125345/

相关文章:

python - Django:列表显示管理中的外键值

python - django modelchoicefield 与未保存的模型

python - raise TypeError('在 include() 的情况下, View 必须是可调用的或列表/元组

python - 如何制作一个 Python 程序来演示 Zipf 定律?

django - ImportError : Failed to import test module:

python - 具有多个数据库的 Django,来自管理员中非默认数据库权限的模型

python - 为什么我收到 IntegrityError : NOT NULL constraint failed

python - 从 mod_wsgi django 应用程序导入 numpy 时偶尔出现 ctypes 错误

python - 从多个列表中获取特定的字典属性值

python-3.x - 如何使用 AWS 证书管理器验证域