当前位置: 首页 > 知识库问答 >
问题:

错误“NoneType”对象没有属性“__dict__”

施飞驰
2023-03-14

我遇到了这个错误,它不允许我在表单中保存信息。初始数据在表单中显示良好,但保存起来很困难。希望有人能帮忙,我真的被困了

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

追踪:

内部 41 中的文件“C:\程序文件\Python35\lib\site-packages\django\core\处理程序\exception.py”。响应 = get_response(请求)

文件"C:\Program Files\Python35\lib\site-pack\django\core\handler\base.py"在_legacy_get_response249。响应=自身。_get_response(请求

_get_response 187 中的文件“C:\程序文件\Python35\lib\site-packages\django\core\处理程序\base.py”。响应 = self.process_exception_by_middleware(e, 请求)

文件"C:\Program Files\Python35\lib\site-pack\django\core\handler\base.py"在_get_response185。响应=wrapped_callback(请求,*callback_args,**callback_kwargs)

文件"C:\Program Files\Python35\lib\site-pack\django\view\通用\base.py"在视图68.返回self.dispatch(请求,*参数,**kwargs)

调度88中的文件“C:\ Program Files \ python 35 \ lib \ site-packages \ django \ views \ generic \ base . py”。返回处理程序(请求,*参数,*kwargs)

在post 217中文件"C:\Program Files\Python35\lib\site-pack\django\view\通用\edit.py"。返回超级(BaseCreateView, Self). post(请求,参数,参数)

183后的文件“C:\Program Files\Python35\lib\site packages\django\views\generic\edit.py”。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-packs\django\view\通用\edit.py"在get_success_url148. url=self.success_url.format(**self.object.dict)

异常类型:/profile/-.1处的attribute error/create异常值:“NoneType”对象没有属性“dict”

共有1个答案

齐才艺
2023-03-14

您已经重写了form_valid方法,但还没有执行该方法执行的任何默认操作,特别是保存对象。

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

 类似资料:
  • 我是硒网络驱动程序的新手,目前使用Python来编写脚本。现在我想应用参数,我使用Excel应用了数据驱动方法。基本上只有第一个循环ok,它可以读取和写入第一行的数据,但之后不能。 首先,我在Python文件中创建它(作为新模块): 接下来,我编写以下代码以在登录和注销过程中包含数据驱动测试: 我得到了错误属性错误:“NoneType”对象没有属性“send_keys”,所以我认为包括等待/睡眠可

  • 如果pk_col值为空,则应打印未定义的主键。但我得到了这个错误。“NoneType”对象没有属性“rdd”。

  • 问题内容: 下面的代码给出了错误: 码: 问题答案: 从代码中,我可以看到你希望允许用户下载pdf。 现在开始 去 http://localhost:5000

  • 问题内容: 我的程序看起来像 当我运行它时,它会抛出我 不知道为什么会这样?当我已经在每个列表的开头创建列表时 问题答案: 实际上,您存储在此处: 更改列表并返回 例:

  • 问题内容: 我遇到了这个问题,我不明白为什么。 我从我的应用程序中获取了代码,并制作了此测试代码,因此您不必费劲地查看我的要求。 我有这个工作在其他代码。但是,在将两者进行比较之后,我无法为自己的一生解决这个问题。 在此应用程序中,出现错误“ AttributeError:’NoneType’对象没有属性’delete’”。 问题答案: 在这一行: grid不返回任何内容,因此entryBox是,

  • 我的模型在Slagify系列中有问题 我试着把代码self.slug=slugify(“slug的测试”)放进去,但问题仍然存在,但当他说保留信息时,没有问题 这是我的错误: 环境: 请求方式:POST请求URL:http://xxx.xx.xx.xx:8000/admin/pages/pages/add/ Django版本:1.7.1 Python版本:3.4.2安装的应用程序:('Django