Django 将字符串转换为整数

Django cast string to Integer

我正在尝试使用以下代码将值从我的模板转换为视图:

<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 而不会出现此错误,也许还有另一种转换它的方法。

当您提交具有相同视图的表单时,您可能同时呈现带有表单的页面和带有结果的页面。

因此,当您第一次呈现该视图时,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', <b>{'length': length}</b>)
    return …

然后用以下方式渲染:

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

不过,我建议 work with forms 这些可用于呈现、验证和清理数据。