B
B
Balundula2022-04-13 00:22:51
Flask
Balundula, 2022-04-13 00:22:51

How to remove "GET / HTTP/1.1" 404 error - Flask?

When trying to access the server (any link) of the Flask application, an error pops up:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

PS If all the code is placed in one file, everything will work.

Here is the code(__init__):

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from flask_login import LoginManager

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
login_manager = LoginManager()


models:
from sweater import db, login_manager


from flask_login import UserMixin

from datetime import datetime


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    login = db.Column(db.String(128), nullable=False, unique=True)
    password = db.Column(db.String(128), nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)


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


routes:
from flask import render_template, request, flash, redirect
from werkzeug.security import generate_password_hash, check_password_hash

from flask_login import login_user

from sweater import db, app
from models import User




@app.route('/')
def index():
    return render_template('base.html')


@app.route('/register', methods=['POST', 'GET'])
def register():
    login = request.form.get('login')
    password = request.form.get('pass')
    password2 = request.form.get('pass2')

    if request.method == 'POST':
        if not (login or password or password2):
            flash('Пожалуйста заполните поля')
        elif password != password2:
            flash('Пароли не совпадают')
        else:
            new_user = User(login=login, password=generate_password_hash(password))
            try:
                db.session.add(new_user)
                db.session.commit()
                return redirect('/login')

            except:
                flash('При регистрации произошла ошибка')

    else:
        return render_template('register.html')


@app.route('/login', methods=['POST', 'GET'])
def login_page():
    login = request.form.get('login')
    password = request.form.get('pass')

    if login and password:
        user = User.query.filter_by(login=login).first()
        if check_password_hash(user.passwoord, password):
            login_user(user)
            return 'Вы вошли'
        else:
            flash('Вы ввели не верные данные')
    else:
        return render_template('login.html')


@app.route('/logout')
def logout():
    pass


@app.route('/profile/<int:user_id>')
def profile(user_id):
    return render_template ('profile.html',  user_id=user_id)


@app.route('/db')
def db_page():
    users = User.query.order_by(User.date).all()
    return render_template('db.html', users=users)


@app.route('/test')
def test():
    test = User.query.filter_by(login='Glipher').first()
    return render_template('test.html', test=test)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question