【问题标题】:error 'NoneType' object has no attribute '__dict__'错误“NoneType”对象没有属性“__dict__”
【发布时间】:2017-12-20 21:01:07
【问题描述】:

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

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

追溯:

文件 "C:\Program Files\Python35\lib\site-packages\django\core\handlers\exception.py" 在 内 41. response = get_response(request)

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

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

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

文件 "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" 在视图中 68. return self.dispatch(request, *args, **kwargs)

文件 "C:\Program Files\Python35\lib\site-packages\django\views\generic\base.py" 在 派遣 88. return handler(request, *args, **kwargs)

文件 "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" 在帖子中 217. return super(BaseCreateView, self).post(request, *args, **kwargs)

文件 "C:\Program Files\Python35\lib\site-packages\django\views\generic\edit.py" 在帖子中 183. return self.form_valid(form)

文件 "C:\Users\wahab\Desktop\site1\ostra\ostrakodecommerce\posts\views.py" 在 form_valid 207.返回HttpResponseRedirect(self.get_success_url())

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

异常类型:AttributeError at /profile/-.​​1/create 异常值: “NoneType”对象没有属性“dict

【问题讨论】:

  • 尝试打印 user_slug,我猜它会出现空白
  • 实际上,一个做得很好,它提供了初始数据,在表格​​中显示得很好
  • 挑战在于保存表单中已经存在的数据
  • 请发布完整的回溯,它显示了发生此错误的位置。
  • @Daniel Roseman 追溯发布

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


【解决方案1】:

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

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

【讨论】:

  • 添加到上面,如果你想重定向而不保存记录,那么你应该扩展FormView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 2021-02-17
  • 2023-02-17
  • 2022-01-12
  • 2020-08-26
  • 1970-01-01
相关资源
最近更新 更多