Gagan0141 commited on
Commit
5b5f56a
·
verified ·
1 Parent(s): 36e0458

Upload 10 files

Browse files
README.md CHANGED
@@ -1,10 +1,13 @@
1
- ---
2
- title: A
3
- emoji: 🚀
4
- colorFrom: yellow
5
- colorTo: yellow
6
- sdk: docker
7
- pinned: false
8
- ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
1
+ # flask-work
 
 
 
 
 
 
 
2
 
3
+ ## 🚀 Features
4
+ - User login form (username and password).
5
+ - Authentication handling in `app.py`.
6
+ - On successful login, users are redirected to a **Welcome Page**.
7
+ - Basic form validation.
8
+ - Clean and simple HTML templates for login and welcome pages.
9
+
10
+ ## ⚡ How to Run
11
+ 1. Clone this repository:
12
+ ```bash
13
+ git clone https://github.com/username/your-flask-login-project.git
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for, session, flash
2
+ from flask_sqlalchemy import SQLAlchemy
3
+ from werkzeug.security import generate_password_hash, check_password_hash
4
+
5
+ # Initialize Flask app
6
+ app = Flask(__name__)
7
+ app.secret_key = 'your_super_secret_key'
8
+
9
+ # Database configuration
10
+ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
11
+ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
12
+
13
+ # Initialize SQLAlchemy
14
+ db = SQLAlchemy(app)
15
+
16
+ # ---------------------
17
+ # Database Models
18
+ # ---------------------
19
+
20
+ class User(db.Model):
21
+ id = db.Column(db.Integer, primary_key=True)
22
+ username = db.Column(db.String(80), unique=True, nullable=False)
23
+ password = db.Column(db.String(200), nullable=False)
24
+ feedbacks = db.relationship('Feedback', backref='user', lazy=True)
25
+
26
+ class Feedback(db.Model):
27
+ id = db.Column(db.Integer, primary_key=True)
28
+ message = db.Column(db.Text, nullable=False)
29
+ user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
30
+
31
+ # ---------------------
32
+ # Routes
33
+ # ---------------------
34
+
35
+ @app.before_request
36
+ def create_tables():
37
+ db.create_all()
38
+
39
+ @app.route('/')
40
+ def home():
41
+ return redirect(url_for('register'))
42
+
43
+ @app.route('/register', methods=['GET', 'POST'])
44
+ def register():
45
+ if request.method == 'POST':
46
+ username = request.form['username']
47
+ password = request.form['password']
48
+
49
+ existing_user = User.query.filter_by(username=username).first()
50
+ if existing_user:
51
+ flash('Username already exists. Please choose another.', 'danger')
52
+ else:
53
+ hashed_pw = generate_password_hash(password)
54
+ new_user = User(username=username, password=hashed_pw)
55
+ db.session.add(new_user)
56
+ db.session.commit()
57
+ flash('Registration successful! Please login.', 'success')
58
+ return redirect(url_for('login'))
59
+ return render_template('register.html')
60
+
61
+ @app.route('/login', methods=['GET', 'POST'])
62
+ def login():
63
+ if request.method == 'POST':
64
+ username = request.form['username']
65
+ password = request.form['password']
66
+
67
+ user = User.query.filter_by(username=username).first()
68
+ if user and check_password_hash(user.password, password):
69
+ session['user'] = user.username
70
+ flash('Logged in successfully!', 'success')
71
+ return redirect(url_for('welcome'))
72
+ else:
73
+ flash('Invalid credentials. Try again.', 'danger')
74
+ return render_template('login.html')
75
+
76
+ @app.route('/welcome')
77
+ def welcome():
78
+ if 'user' not in session:
79
+ flash('Please log in first.', 'warning')
80
+ return redirect(url_for('login'))
81
+ return render_template('welcome.html', username=session['user'])
82
+
83
+ @app.route('/feedback', methods=['GET', 'POST'])
84
+ def feedback():
85
+ if 'user' not in session:
86
+ flash('Please log in first.', 'warning')
87
+ return redirect(url_for('login'))
88
+
89
+ user = User.query.filter_by(username=session['user']).first()
90
+
91
+ if request.method == 'POST':
92
+ msg = request.form['message']
93
+ feedback_entry = Feedback(message=msg, user=user)
94
+ db.session.add(feedback_entry)
95
+ db.session.commit()
96
+ flash('Feedback received! Thank you.', 'success')
97
+ return render_template('feedback.html', message=msg)
98
+
99
+ return render_template('feedback.html', message=None)
100
+
101
+ @app.route('/logout')
102
+ def logout():
103
+ session.pop('user', None)
104
+ flash('Logged out successfully.', 'info')
105
+ return redirect(url_for('login'))
106
+
107
+ # ---------------------
108
+ # Run the app
109
+ # ---------------------
110
+
111
+ if __name__ == '__main__':
112
+ app.run(debug=True)
instance/database.db ADDED
Binary file (16.4 kB). View file
 
models.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask_sqlalchemy import SQLAlchemy
2
+
3
+ db = SQLAlchemy()
4
+
5
+ class User(db.Model):
6
+ id = db.Column(db.Integer, primary_key=True)
7
+ username = db.Column(db.String(80), unique=True, nullable=False)
8
+ password = db.Column(db.String(120), nullable=False)
9
+
10
+ feedbacks = db.relationship('Feedback', backref='user', lazy=True)
11
+
12
+ class Feedback(db.Model):
13
+ id = db.Column(db.Integer, primary_key=True)
14
+ feedback = db.Column(db.Text, nullable=False)
15
+
16
+ user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask
2
+ Flask-SQLAlchemy
3
+ Flask-Migrate
4
+ requests
5
+ pytest
templates/base.html ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF‑8">
5
+ <meta name="viewport" content="width=device‑width, initial‑scale=1">
6
+ <title>Flask Awesome App</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
9
+ </head>
10
+ <body class="bg-light">
11
+ <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
12
+ <div class="container">
13
+ <a class="navbar-brand" href="{{ url_for('register') }}">MyFlaskApp</a>
14
+ <div class="collapse navbar-collapse">
15
+ <ul class="navbar-nav ms-auto">
16
+ {% if session.get('user') %}
17
+ <li class="nav-item"><a class="nav-link" href="{{ url_for('logout') }}">Logout</a></li>
18
+ {% else %}
19
+ <li class="nav-item"><a class="nav-link" href="{{ url_for('login') }}">Login</a></li>
20
+ <li class="nav-item"><a class="nav-link" href="{{ url_for('register') }}">Register</a></li>
21
+ {% endif %}
22
+ </ul>
23
+ </div>
24
+ </div>
25
+ </nav>
26
+
27
+ <div class="container mt-5">
28
+ {% with messages = get_flashed_messages(with_categories=true) %}
29
+ {% if messages %}
30
+ {% for category, msg in messages %}
31
+ <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
32
+ {{ msg }}
33
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
34
+ </div>
35
+ {% endfor %}
36
+ {% endif %}
37
+ {% endwith %}
38
+
39
+ {% block content %}{% endblock %}
40
+ </div>
41
+
42
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
43
+ </body>
44
+ </html>
templates/feedback.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'base.html' %}
2
+ {% block content %}
3
+ <div class="row justify-content-center">
4
+ <div class="col-md-8">
5
+ <div class="card shadow">
6
+ <div class="card-body p-5">
7
+ {% if message %}
8
+ <h2 class="text-center mb-4">Thank You!</h2>
9
+ <p class="lead text-center">{{ message }}</p>
10
+ {% else %}
11
+ <h2 class="card-title text-center mb-4">We’d love your feedback</h2>
12
+ <form method="post">
13
+ <div class="mb-3">
14
+ <label for="message" class="form-label">Your Message</label>
15
+ <textarea class="form-control" id="message" name="message" rows="5" required></textarea>
16
+ </div>
17
+ <button type="submit" class="btn btn-outline-primary w-100">Submit</button>
18
+ </form>
19
+ {% endif %}
20
+ </div>
21
+ </div>
22
+ </div>
23
+ </div>
24
+ {% endblock %}
templates/login.html ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'base.html' %}
2
+ {% block content %}
3
+ <div class="row justify-content-center">
4
+ <div class="col-md-6">
5
+ <div class="card shadow">
6
+ <div class="card-body p-5">
7
+ <h2 class="card-title text-center mb-4">Login</h2>
8
+ <form method="post">
9
+ <div class="mb-3">
10
+ <label for="username" class="form-label">Username</label>
11
+ <input type="text" class="form-control" id="username" name="username" required>
12
+ </div>
13
+ <div class="mb-3">
14
+ <label for="password" class="form-label">Password</label>
15
+ <input type="password" class="form-control" id="password" name="password" required>
16
+ </div>
17
+ <button type="submit" class="btn btn-success w-100">Login</button>
18
+ </form>
19
+ <p class="mt-3 text-center">
20
+ New? <a href="{{ url_for('register') }}">Register here</a>
21
+ </p>
22
+ </div>
23
+ </div>
24
+ </div>
25
+ </div>
26
+ {% endblock %}
templates/register.html ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'base.html' %}
2
+ {% block content %}
3
+ <div class="row justify-content-center">
4
+ <div class="col-md-6">
5
+ <div class="card shadow">
6
+ <div class="card-body p-5">
7
+ <h2 class="card-title text-center mb-4">Register</h2>
8
+ <form method="post">
9
+ <div class="mb-3">
10
+ <label for="username" class="form-label">Username</label>
11
+ <input type="text" class="form-control" id="username" name="username" required>
12
+ </div>
13
+ <div class="mb-3">
14
+ <label for="password" class="form-label">Password</label>
15
+ <input type="password" class="form-control" id="password" name="password" required>
16
+ </div>
17
+ <button type="submit" class="btn btn-primary w-100">Register</button>
18
+ </form>
19
+ <p class="mt-3 text-center">
20
+ Already have an account? <a href="{{ url_for('login') }}">Login</a>
21
+ </p>
22
+ </div>
23
+ </div>
24
+ </div>
25
+ </div>
26
+ {% endblock %}
templates/welcome.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'base.html' %}
2
+ {% block content %}
3
+ <div class="text-center">
4
+ <h1 class="display-4">Welcome, {{ username }}!</h1>
5
+ <p class="lead">We're thrilled to have you here.</p>
6
+ <p class="lead mt-4 text-success fw-semibold text-center">
7
+ This is my very first website built with <strong>Flask</strong>, and I’m super excited to share it with you! 🚀<br>
8
+ It has been a great learning experience, and I'm proud to see it all come together. <br>
9
+ I've added some clean styles and interactive elements to make the site more engaging and user-friendly. <br>
10
+ Your feedback means a lot — it’ll help me grow and build even better projects in the future!
11
+ </p>
12
+
13
+ <p class="lead mt-4">
14
+ We're excited to have you on board! This platform is designed to give you a smooth and enjoyable experience as you explore everything we have to offer. Whether you're here to learn, connect, or share your thoughts — you've just joined a growing community that values your voice. Feel free to navigate around, and when you're ready, we'd love to hear your feedback. Let's make something great together!
15
+ </p>
16
+ <a href="{{ url_for('feedback') }}" class="btn btn-warning btn-lg mt-4">Give Feedback</a>
17
+ </div>
18
+ {% endblock %}