如何检查用户是否只输入数字

问题描述 投票:0回答:2

我正在尝试更新我的注册系统。我希望用户输入他的年龄,但我不知道如何检查用户是否仅输入数字。我将给出我的代码片段,它不起作用

我的观点.py:

def Registration(request):
    if request.method == "POST":
        username = request.POST["username"]
        age = request.POST["age"]

        ...

        if username.isnumeric() == "True":
            messages.error(request, "Username can't be all number!")
            return redirect("/")
        if age.isnumeric() == "False":
            messages.error(request, "Age must be all numbers!")
            return redirect("/")
        
        ...

        messages.success(request, "Your account has been succesfully created")

        return redirect("/Login/")
    return render(request, "Registration.html")

我的注册.html:

{% load static %}
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8" />

    <meta name="viewport" content="width-device-width, initial-scale=1, maximum-scale=1" />

    <link rel="stylesheet" type="text/css" href="https://bootswatch.com/5/solar/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">

    <title> view tasks </title>

</head>

<body>
    <div class="text-center">

        <h1>Registrate here</h1>
        <form action="/Registration/" method="post">
            {% csrf_token %}
            <label for"username">Username</label>
            <br>
            <input class="" type="text" id="username" name="username" placeholder="Create a username (letter and numbers only)" Required>
            <br> <br>

            <label for"age">Age</label>
            <br>
            <input class="" type="text" id="age" name="age" placeholder="Enter your age" Required>
            <br> <br>

            <label for"fname">First Name</label>
            <br>
            <input class="" type="text" id="fname" name="fname" placeholder="Enter your first name" Required>
            <br> <br>

            <label for"lname">Last Name</label>
            <br>
            <input class="" type="text" id="lname" name="lname" placeholder="Enter your last name" Required>
            <br> <br>

            <label for"email">Email</label>
            <br>
            <input class="" type="email" id="email" name="email" placeholder="Enter your email" Required>
            <br> <br>

            <label for"pass1">Password</label>
            <br>
            <input class="" type="password" id="pass1" name="pass1" placeholder="Create your password" Required>
            <br> <br>

            <label for"pass2">Confirm Password</label>
            <br>
            <input class="" type="password" id="pass2" name="pass2" placeholder="Confirm your password" Required>
            <br> <br>

            <button type"submit"> Registrate </button>
        </form>
    </div>
</body>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>



<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
    integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous">

    </script>

</html>

我尝试研究可以解决我的问题的函数,但我没有找到任何东西,也许看起来不够。问题不在于引发错误,它在其他 if 上起作用。 Age.isnumeric() 没有像我想象的那样工作。

python django function django-views django-templates
2个回答
1
投票

isnumeric() 方法返回 True 或 False,作为布尔值,而不是字符串,因此您应该删除“”并具有类似的内容:

if username.isnumeric() == True:
# THE REST OF YOUR CODE 

0
投票

您可以从

更新
registration.html

中的输入字段
<input class="" type="text" id="age" name="age" placeholder="Enter your age" Required>

<input type="number" id="age" name="age" min="10" max="100" />

您可以在这里阅读更多相关信息

© www.soinside.com 2019 - 2024. All rights reserved.