【问题标题】:'tuple' object has no attribute 'get' attribute error'tuple' 对象没有属性 'get' 属性错误
【发布时间】:2018-08-17 06:30:31
【问题描述】:
 add_form = CarAddForm()
    if request.method == "POST":
        add_form = CarAddForm(data=(request.POST, request.FILES) or None)
        if add_form.is_valid:
            prof = add_form.save(commit=False)
            prof.profile = request.user.profile
            prof.purchase_yr = str(request.POST.get('purc_date'))
            prof.save()
            print("abc")
            print(add_form)
            messages.success(request,'Details saved successfully',  extra_tags='alert')
            return redirect('/')
        else:
            add_form = CarAddFormCar()
            messages.error(request,'Something went wrong!',  extra_tags='alert')

prof = add_form.save(commit=False) 正在制造问题。 **prof = add_form.save(commit=False) 正在制造问题。 **

【问题讨论】:

    标签: django python-3.x tuples


    【解决方案1】:

    这不是用数据实例化表单的方式。做吧:

    add_form = CarAddForm(request.POST, request.FILES)
    

    另外注意,is_valid是一个方法,你需要调用它:

    if add_form.is_valid():
    

    如果表单无效,则将表单重新创建为空是没有意义的;用户只会看到您模糊的“出现问题”消息,但不会知道 什么 出了问题。删除 else 块中的 add_form = CarAddFormCar()

    【讨论】:

    • 谢谢我对 django 和 python 很陌生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 2020-11-07
    • 2022-07-17
    • 2022-01-14
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多