database login.py and register.py error showing 404 file not found and doesn't work

26 views Asked by At

I want to connect to database and do normal login and registration process

  • details from registration should be stored in data.db and can be retrieved by login for login purpose that is what i want but as i am new to all database and stuff so its kinda hard

This is Directories for all

This is my Registration html -

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Signup</title>
    <link rel="stylesheet" href="style_signup.css">
</head>

<body>
    <header>
        <h1 class="logo">Jigoku</h1>
        <nav class="navigation">
            <a href="../index.html">Home</a>
            <a href="../About/about.html">About</a>
            <a href="../Download/download.html">Download</a>
            <a href="" class="active">Signup</a>
        </nav>
    </header>

    <section>
        <img src="/signup/background.jpg" class="back">
    </section>

    <div class="wrapper">
        <div class="form-box">
            <h2>Register</h2>
            <form action="/signup/Register/register.py" method="post">
                <div class="input-box">
                    <span class="icon"><ion-icon name="Person"></ion-icon></span>
                    <input type="text" name="username" required>
                    <label>Username</label>
                </div>
                <div class="input-box">
                    <span class="icon"><ion-icon name="mail"></ion-icon></span>
                    <input type="email" name="email" required>
                    <label>Email</label>
                </div>
                <div class="input-box">
                    <input type="password" name="password" id="password" required>
                    <label for="password">Password</label>
                    <ion-icon name="eye" class="icon" onclick="togglePasswordVisibility()"></ion-icon>
                </div>                
                <div class="remember-forget">
                    <label><input type="checkbox" name="agree">Agree to T&Cs</label>
                    <a href="../../About/about.html">T&cs Documentations</a>
                </div>
                <button type="submit" class="btn">Register</button>
                <div class="login-register">
                    <p>Already have an account?<a href="../index_signup.html" class="register-link">Login</a></p>
                </div>
            </form>
        </div>
    </div>

    <div class="wrapper">
        <div class="form-box"></div>
    </div>
    <script src="script_signup.js"></script>
    <script type="module" src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js"></script>
    <script nomodule src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.js"></script>
</body>

</html>

This is my login html -

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="style_signup.css">
</head>

<body>
    <header>
        <h1 class="logo">Jigoku</h1>
        <nav class="navigation">
            <a href="../index.html">Home</a>
            <a href="../About/about.html">About</a>
            <a href="../Download/download.html">Download</a>
            <a href="" class="active">Login</a>
        </nav>
    </header>

    <section>
        <img src="/signup/background.jpg" class="back">
    </section>

    <div class="wrapper">
        <div class="form-box">
            <h2>Login</h2>
            <form action="/signup/login.py" method="post">
                <div class="input-box">
                    <span class="icon"><ion-icon name="mail"></ion-icon></span>
                    <input type="email" name="email" required>
                    <label>Email</label>
                </div>
                <div class="input-box">
                    <input type="password" name="password" id="password" required>
                    <label for="password">Password</label>
                    <ion-icon name="eye" class="icon" onclick="togglePasswordVisibility()"></ion-icon>
                </div>                
                <div class="remember-forget">
                    <label><input type="checkbox" name="remember">Remember me</label>
                    <a href="#">Forgot password?</a>
                </div>
                <button type="submit" class="btn">Login</button>
                <div class="login-register">
                    <p>Don't have an account?<a href="./Register/index_signup.html" class="register-link">Register</a></p>
                </div>
            </form>
        </div>
    </div>

    <div class="wrapper">
        <div class="form-box"></div>
    </div>
    <script src="script_signup.js"></script>
    <script type="module" src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js"></script>
    <script nomodule src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.js"></script>
</body>

</html>

And here is my register.py file

from flask import Flask, render_template, request, jsonify
import sqlite3

app = Flask(__name__)

@app.route('/signup/Register/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form['username']
        email = request.form['email']
        password = request.form['password']

        conn = sqlite3.connect('../users.db')
        cursor = conn.cursor()

        try:
            cursor.execute('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', (username, email, password))
            conn.commit()
            conn.close()
            return jsonify({'success': True, 'message': 'Registration successful'})
        except sqlite3.IntegrityError:
            conn.close()
            return jsonify({'success': False, 'message': 'Username or email already exists'})
    else:
        return render_template('signup/Register/index_signup.html')

if __name__ == '__main__':
    app.run(debug=True)

and login.py file

from flask import Flask, request, jsonify, redirect
import sqlite3

app = Flask(__name__)

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

    conn = sqlite3.connect('users.db')
    cursor = conn.cursor()

    cursor.execute('SELECT * FROM users WHERE email=? AND password=?', (email, password))
    user = cursor.fetchone()

    conn.close()

    if user:
        # Redirect to a success page
        return redirect('/success')
    else:
        # Redirect to a failure page
        return redirect('/failure')

if __name__ == '__main__':
    app.run(debug=True)

I tried running the python files and it give 404 error of file not found after asking GPT it said it was most likely @app.route(/register) where the problem is but i am new to flask and don't know what is this

0

There are 0 answers