Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

590
Views
Flask Web App, Registration Page Gives Error404 Error When Adding New User

I am trying to create a flask web application with registration form and database connection. All the features in the app are working however only the registration page has stopped working now. It was working all right before. I have spent too much time to debug, without any success. Please help.

Here is the routes.py:

from flask import render_template, url_for, flash, redirect, request
from sms_main import app, db, bcrypt
from sms_main.forms import RegistrationForm, LoginForm
from sms_main.models import User, Post
from flask_login import login_user, current_user, logout_user, login_required


sms_profiles = [
    {
        'skill': 'Home Cook',
        'name': 'Ahad',
        'price': '$ 8.00 per meal'
    },
    {
        'skill': 'Barber',
        'name': 'Ahad',
        'price': '$ 10.00 per haircut'
    }
    ]

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

@app.route("/about")
def about():
    return render_template('about.html', title = 'About')

@app.route("/register", methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success') 
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

@app.route("/login", methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))
        else:
            flash('Login Unsuccessful. Pleas check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)

@app.route("/logout")
def logout():
    logout_user()
    return redirect(url_for('home'))

@app.route("/account")
@login_required
def account():
    return render_template('account.html', title='Account')

Here is models.py:

from datetime import datetime
from sms_main import db, login_manager
from flask_login import UserMixin

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)    
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Post('{self.title}'. '{self.date_posted}')"

Here is forms.py:

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

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

def validate_username(self, username):

    user = User.query.filter_by(username=username.data).first()
    if user:
        raise ValidationError('That username is already taken. Choose different one.')

def validate_email(self, email):
    user = User.query.filter_by(email=email.data).first()
if user:
    raise ValidationError('That email is taken. Choose another one.')
class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')

Here is register.html:

    {% extends "layout.html" %}
    {% block content %}
        <div class="content-section">
            <form method="POST" action="GET">
                {{ 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") }}
                        {% if form.username.errors %}
                            {{ form.username(class="form-control form-control-lg is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.username.errors %}       
                                    <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                        {% else %}
                            {{ form.username(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                    <div class="form-group">
                        {{ form.email.label(class="form-control-label") }}
                        {% if form.email.errors %}
                            {{ form.email(class="form-control form-control-lg is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.email.errors %}       
                                    <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                        {% else %}
                            {{ form.email(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                    <div class="form-group">
                        {{ form.password.label(class="form-control-label") }}
                        {% if form.password.errors %}
                            {{ form.password(class="form-control form-control-lg is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.password.errors %}       
                                    <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                        {% else %}
                            {{ form.password(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                    <div class="form-group">
                        {{ form.confirm_password.label(class="form-control-label") }}
                        {% if form.confirm_password.errors %}
                            {{ form.confirm_password(class="form-control form-control-lg is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.confirm_password.errors %}       
                                    <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                        {% else %}
                            {{ form.confirm_password(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                </fieldset>
                <div class="form-group">
                    {{ form.submit(class="btn btn-outline-info") }}
                </div>
            </form>
        </div>
        <div class="border-top pt-3">
           <small class="text-muted">
               Already have an account? <a class="ml-2" href="{{ url_for('login') }}">Sign In</a>
            </small> 
        </div>
    {% endblock content %}

Here is layout.html:

<!DOCTYPE html>
<html>
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">

  {% if title %}
      <title>SMS:{{title}}</title>
  {% else %}
      <title>SMS:Selk My Skill</title>
  {% endif %}
</head>
<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="/">Sell My Skill</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="{{ url_for('home') }}">Home</a>
            <a class="nav-item nav-link" href="{{ url_for('about') }}">About</a>
          </div>
          <!-- Navbar Right Side -->
          <div class="navbar-nav">
            {% if current_user.is_authenticated %}
              <a class="nav-item nav-link" href="{{ url_for('account') }}">Account</a>
              <a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>
            {% else %}
              <a class="nav-item nav-link" href="{{ url_for('login') }}">Login</a>
              <a class="nav-item nav-link" href="{{ url_for('register') }}">Register</a>
            {% endif %}
          </div>
        </div>
      </div>
    </nav>
  </header>
  <main role="main" class="container">
    <div class="row">
      <div class="col-md-8">
        {% with messages = get_flashed_messages(with_categories=true) %}
          {% if messages %}
            {% for category, message in messages %}
              <div class="alert alert-{{ category }}">
                {{ message }}
              </div>
            {% endfor %}
          {% endif %} 
        {% endwith %}
        {% block content %}{% endblock %}
      </div>
      <div class="col-md-4">
        <div class="content-section">
          <h3>Our Sidebar</h3>
          <p class='text-muted'>You can put any information here you'd like.
            <ul class="list-group">
              <li class="list-group-item list-group-item-light">Latest Posts</li>
              <li class="list-group-item list-group-item-light">Announcements</li>
              <li class="list-group-item list-group-item-light">Calendars</li>
              <li class="list-group-item list-group-item-light">etc</li>
            </ul>
          </p>
        </div>
      </div>
    </div>
  </main>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I believe the issue resides with the register template form. The form action attribute is currently "get"

  <form method="POST" action="GET">

When it should be the absolute or relative URL of where to send the form data.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!