Django将字符串转换为整数

2024-07-07 06:51:53 发布

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

我试图使用以下代码将我的模板中的值强制转换为视图:

<form action="{% url 'view_passwordgenerator' %}">
<select name="length"> 
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12" selected="selected">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
</select> Length
<input type="submit" value="Generate Password" class="btn btn-primary">
</form>```

观点

def view_passwordgenerator(request):
    length = int(request.GET.get('length'))
    for i in range(length):
        ...

    return render(request, 'home/passwordgenerator.html')

出现以下错误:

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

我只是在寻找一种方法,在没有这个错误的情况下,从模板中获取int,也许还有另一种方法可以转换它


Tags: 方法代码formview模板valuerequest错误
1条回答
网友
1楼 · 发布于 2024-07-07 06:51:53

在使用相同视图提交表单时,可能会同时呈现包含表单的页面和包含结果的页面

因此,当您第一次呈现该视图时,length将丢失,因此您应该检查并仅将其转换为int,如果我们知道它是字典的一部分:

def view_passwordgenerator(request):
    if 'length' in request.GET:
        length = int(request.GET.get('length'))
        for i in range(length):
            # …
        return render(request, 'home/passwordgenerator.html')
    return …

然而,这仍然不是很优雅,因为人们可以使用URL将数据注入request.GET,而这本身并不是一个int。最优雅的方式可能是使用Django表单:

from django import forms

class MyForm(forms.Form):
    length = forms.IntegerField()

然后,您可以检查表单是否验证:

def view_passwordgenerator(request):
    form = MyForm(request.GET)
    if form.is_valid():
        for i in range(form.cleaned_data['length']):
            # …
        return render(request, 'home/passwordgenerator.html')
    return …

可以通过将拾取的项目传递到上下文来渲染该项目:

def view_passwordgenerator(request):
    if 'length' in request.GET:
        length = int(request.GET.get('length'))
        for i in range(length):
            # …
        return render(request, 'home/passwordgenerator.html', {'length': length})
    return …

然后使用以下命令渲染此内容:

<select name="length"> 
    <option value="8" {% if length == 8 %}selected{% endif %}>8</option>
    <option value="9" {% if length == 9 %}selected{% endif %}>9</option>
    <option value="10" {% if length == 10 %}selected{% endif %}>10</option>
    <option value="11" {% if length == 11 %}selected{% endif %}>11</option>
    <option value="12" {% if length == 12 %}selected{% endif %}>12</option>
    <option value="13" {% if length == 13 %}selected{% endif %}>13</option>
    <option value="14" {% if length == 14 %}selected{% endif %}>14</option>
    <option value="15" {% if length == 15 %}selected{% endif %}>15</option>
    <option value="16" {% if length == 16 %}selected{% endif %}>16</option>
    <option value="17" {% if length == 17 %}selected{% endif %}>17</option>
    <option value="18" {% if length == 18 %}selected{% endif %}>18</option>
    <option value="19" {% if length == 19 %}selected{% endif %}>19</option>
    <option value="20" {% if length == 20 %}selected{% endif %}>20</option>
</select>

但是,我建议work with forms这些可以用于呈现、验证和清理数据

相关问题 更多 >