int()参数必须是字符串、类似字节的对象或数字,而不是'NoneType'Django形式

2024-04-16 11:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在签出视图中提交表单时遇到问题。 首先,我在我的主视图上有一个表单,其中我要求一个项目的数量。我将数据发布到我的签出视图,以便在前端进行摘要。 在我的结帐视图中,我有另一个表单,其中我要求提供电子邮件和姓名,当我尝试提交结帐表单时,我有一个打字错误

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

这是我的结账视图

# My view 
@login_required(login_url='login')
def checkout(request):

    data = request.POST.copy()

    region = data.get('region-select')
    plateform = data.get('plateform-select')
    quantity = data.get('quantity-field')
    quantity = int(quantity)
    price = quantity * 3.99

    context = {
        'region':region,
        'plateform':plateform,
        'quantity':quantity,
        'price':price
    }

    return render(request, 'main/checkout.html', context)

我不明白为什么会发生这种情况,因为数据是在我的模板中呈现的

<h1 class="title is-5" style="margin-bottom:5px;"> {{price|floatformat:2}} EUR </h1>

Tags: 数据视图表单datagetrequestloginselect