0

In finance, I am getting the following error:

sending POST request to /register
exception raised in application: TypeError: expected string or bytes-like object, got 'NoneType'

Why am I getting this error?

Update: Here's the python code of my flask /register route:


@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    # Checklist:
    # 1. Check if the username exists
    # 2. Check if the email is valid
    # 3. Check if the password is valid

    # When user submits a form via POST
    if request.method == "POST":
        # Get the input data by using request.form.get()
        username = request.form.get("username")
        email = request.form.get("h_email")
        password = request.form.get("password")
        p_hash = generate_password_hash(
            password, method="pbkdf2:sha256"
        )  # Generate hash using pbkdf2:sha256

        # 1. Check if the username already exists
        if db.execute("SELECT * FROM users WHERE username = ?", username):
            return (
                render_template("register.html", h_error="Username already exists!"),
                400,
            )  # 400 is http error code
        
        if not re.match(r"^(?=[a-zA-Z])[\w\d]{4,24}$", username):
            return (
                render_template(
                    "register.html",
                    h_error="Username may only contain letters and numbers. Length 4-24 chars!",
                ),
                400,
            )

        # 2. Check if the email is valid
        if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
            return render_template("register.html", h_error="Invalid email!"), 400
        else:
            existing_email = db.execute(
                "SELECT * FROM users WHERE email = ?", email
            )  # 2.1 Check if email exists (bool)
            if existing_email:
                return (
                    render_template("register.html", h_error="Email already exists!"),
                    400,
                )

        # 3. Check if the password is valid (at least 12 characters with both letters and numbers)
        if not re.match(r"^(?=.*[A-Za-z])(?=.*\d).{12,}$", p_hash):
            return (
                render_template(
                    "register.html",
                    h_error="Password must be 12 characters with both letters and numbers!",
                ),
                400,
            )

        # Insert the data to the database
        db.execute(
            "INSERT INTO users (username, email, hash) VALUES (?, ?, ?)",
            username,
            email,
            p_hash,
        )
        return render_template("register.html", h_success="Success!"), 200
    else:
        # Return the register.html webpage when /register is visited
        return render_template("register.html"), 200

Here's the HTML:


{% block main %}

    <!-- Display error if input is invalid -->
    {% if h_error %}
        <div class="alert alert-danger" role="alert">
            {{ h_error }}
        </div>
    {% endif %}

    <form action="/register" method="post" onsubmit="return validateInput()">
        <div class="mb-3">
            <input autocomplete="off" autofocus class="form-control mx-auto w-auto" id="r_username" name="username" placeholder="Username" type="text" oninput="validateInput()">
        </div>
        <div class="mb-3">
            <input class="form-control mx-auto w-auto" id="r_email" name="h_email" placeholder="Email" type="email" oninput="validateInput()">
        </div>
        <div class="mb-3">
            <input class="form-control mx-auto w-auto" id="r_password" name="password" placeholder="Password" type="password" oninput="validateInput()">
        </div>
        <div class="mb-3">
            <input class="form-control mx-auto w-auto" id="r_confirmation" name="confirmation" placeholder="Confirm Password" type="password" oninput="validateInput()">
        </div>
        <button id="r_submit" class="btn btn-primary" type="submit" disabled>Register</button>
    </form>

    <!-- Username & password requirements warning -->
    <div class="mt-4 alert alert-warning" role="alert" id="username-warning" style="display:block;">
        Username must have 4 characters and password 12 characters with both letters and numbers.
    </div>

    <!-- Display success message if input is valid -->
    {% if h_success %}
        <div class="alert alert-success" role="alert">
            {{ h_success }}
        </div>
    {% endif %}

    <!-- Input validation -->
    <script>
        function validateInput() {
            var usernameInput = document.getElementById("r_username");
            var emailInput = document.getElementById("r_email");
            var passwordInput = document.getElementById("r_password");
            var confirmationInput = document.getElementById("r_confirmation");
            var registerBtn = document.getElementById("r_submit");

            // Check username validity (at least 4 characters)
            var isValidUsername = usernameInput.value.length >= 4;

            // Check email validity
            var isValidEmail = /[^@]+@[^@]+\.[^@]+/.test(emailInput.value);

            // Check if the password is valid (at least 12 characters with both letters and numbers)
            var isValidPassword = /^(?=.*[A-Za-z])(?=.*\d).{12,}$/.test(passwordInput.value);

            // Check if passwords match
            var doPasswordsMatch = passwordInput.value === confirmationInput.value;

            // Enable or disable the submit button based on all validation conditions
            registerBtn.disabled = !(isValidUsername && isValidEmail && isValidPassword && doPasswordsMatch);

            // Return false to prevent form submission if the validation fails
            return isValidUsername && isValidEmail && isValidPassword && doPasswordsMatch;
        }
    </script>


{% endblock %}
3
  • we have no way of knowing without any of your code.
    – UpAndAdam
    Dec 2, 2023 at 19:25
  • @UpAndAdam I updated the question with the code. Dec 10, 2023 at 22:56
  • this has nothing to do with quotes. this has to do with your register function not working just like it says. what was the input and what line specifically is throwing the exception? have you checked that the python flask code is actually obtaining a username, password and email? The error means you are calling a function with a Nonetype argument, exactly what it says.
    – UpAndAdam
    Dec 11, 2023 at 4:28

0

You must log in to answer this question.

Browse other questions tagged .