Search code examples
pythondjangofunctiondjango-viewsdjango-templates

How to check if user is entering only numbers


I'm trying to update my registration system. I want user entering his age, but I don't know how to check if user entered only numbers. I will give snippet of my code, where it doesn't work

my views.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")

my registration.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>

I tried researching functions that could solve my problem, but I didn't find anything, maybe didn't look enough. The problem isn't at raising errors, it worked on other ifs. The age.isnumeric() isn't working as I thought it would.


Solution

  • isnumeric() method returns True or False, as a boolean, not a String, hence you should remove the "" and have something like :

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