python – Flask – Submit Button doing nothing-ThrowExceptions

Exception or error:

I am following a tutorial on flask. (First time using it) So I built his little bloglike mini website and it contains a form for registering.

When I run it I get good html, looks like it should work to me. I have no idea why it’s not working. There is not even a request to the server it is as if I hadn’t clicked the button at all.


#main.py
from flask import Flask, render_template, url_for, flash, redirect
from forms import RegistrationForm, LoginForm
app = Flask(__name__)

app.config['SECRET_KEY'] = 'Secret Key'


@app.route("/")
@app.route("/home")
def hello():
    return render_template('home.html', posts=posts)

@app.route("/about")
def about():
    return render_template('about.html')



#THIS IS THE FORM
@app.route("/register", methods=['GET', 'POST'])
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('home'))
    return render_template('register.html', title='Register', form=form)




@app.route("/login")
def login():
    form = LoginForm()
    return render_template('login.html', title='Log In', form=form)


if __name__ == '__main__':
    app.run(debug=True) 
<!-- register.html -->

{% extends "layout.html" %}
{% block content %}
    <div class='content-section'>
        <from method="POST" action="">
            {{ form.hidden_tag() }}
            <fieldset class='form-group'>
                <legend class='border-bottom mb-4'>Join Today</legend>
                <div class="form-group">
                    {{ form.username.label(class="form-control-label") }}
                    {{ form.username(class="form-control form-control-lg") }}

                </div>
                <div class="form-group">
                    {{ form.email.label(class="form-control-label") }}
                    {{ form.email(class="form-control form-control-lg") }}

                </div>
                <div class="form-group">
                    {{ form.password.label(class="form-control-label") }}
                    {{ form.password(class="form-control form-control-lg") }}

                </div>
                <div class="form-group">
                    {{ form.confirm_password.label(class="form-control-label") }}
                    {{ form.confirm_password(class="form-control form-control-lg") }}
                </div>
            </fieldset>
            <div class="form-group">
                {{ form.submit(class="btn btn-outline-info") }}
            </div>
        </from>
    </div>
    <div class ="border-top pt-3">
        <small class="text-muted">
            Alreade Have An Account? <a class="ml-2" href="{{ url_for('login') }}">Sign In</a>
        </small>
    </div>
{% endblock content %}


#forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo


class RegistrationForm(FlaskForm):
    username = StringField('Username',
                           validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')

The result I get is as follows:

<!-- Sourcecode Browser -->
<body>
    <header class="site-header">
      <nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
        <div class="container">
          <a class="navbar-brand mr-4" href="/">Flask Blog</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarToggle">
            <div class="navbar-nav mr-auto">
              <a class="nav-item nav-link" href="/">Home</a>
              <a class="nav-item nav-link" href="/about">About</a>
            </div>
            <!-- Navbar Right Side -->
            <div class="navbar-nav">
              <a class="nav-item nav-link" href="/login">Login</a>
              <a class="nav-item nav-link" href="/register">Register</a>
            </div>
          </div>
        </div>
      </nav>
    </header>


    <main role="main" class="container">
      <div class="row">
        <div class="col-md-8">




    <div class='content-section'>
        <from method="POST" action="">
            <input id="csrf_token" name="csrf_token" type="hidden" value="Huge Secret">
            <fieldset class='form-group'>
                <legend class='border-bottom mb-4'>Join Today</legend>
                <div class="form-group">
                    <label class="form-control-label" for="username">Username</label>
                    <input class="form-control form-control-lg" id="username" name="username" required type="text" value="">

                </div>
                <div class="form-group">
                    <label class="form-control-label" for="email">Email</label>
                    <input class="form-control form-control-lg" id="email" name="email" required type="text" value="">

                </div>
                <div class="form-group">
                    <label class="form-control-label" for="password">Password</label>
                    <input class="form-control form-control-lg" id="password" name="password" required type="password" value="">

                </div>
                <div class="form-group">
                    <label class="form-control-label" for="confirm_password">Confirm Password</label>
                    <input class="form-control form-control-lg" id="confirm_password" name="confirm_password" required type="password" value="">
                </div>
            </fieldset>
            <div class="form-group">
                <input class="btn btn-outline-info" id="submit" name="submit" type="submit" value="Sign Up">
            </div>
        </from>
    </div>
    <div class ="border-top pt-3">
        <small class="text-muted">
            Alreade Have An Account? <a class="ml-2" href="/login">Sign In</a>
        </small>
    </div>


        </div>
        <div class="col-md-4">
          <!-- SIDEBAR -->
        </div>
      </div>
    </main>



    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

First I thought it is because the action argument was empty for form so I tried changing that. Nothing happend.
I removed all the validators, but that didn’t do anything either.
I see in the console that there is no request made to the server. I click submit and literally nothing happens. I tried chrome and edge.
What am I doing wrong?

How to solve:

You have a typo in <from method="POST" action=""> — it should be <form ...> (and action="" is implicit, so just <form method="POST">).

Leave a Reply

Your email address will not be published. Required fields are marked *