DEV Community

Nilesh Raut
Nilesh Raut

Posted on • Edited on

create Login app using flask Framework

Dependency require to install :


`         pip install flask-wtf
       pip install flask-login   
Enter fullscreen mode Exit fullscreen mode
              `
Enter fullscreen mode Exit fullscreen mode

User Registration :

The first step in building a Flask login web app is to create a registration form that allows new users to create an account. We will be using Flask-WTF to build our form. Here's an example registration form:

`
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, 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')

`

In this form, we have four fields: username, email, password, and confirm_password. Each field has a corresponding validator that ensures the field is not empty or meets specific requirements (such as a valid email format). We also have a SubmitField that will allow the user to submit the form.

Next, we need to create a route that will render this form and handle the user's form submission. Here's an example route:

from flask import render_template, redirect, url_for
from your_app import app, db
from your_app.models import User
from your_app.forms import RegistrationForm


@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)

second step : login and Authentication
third step : Logout out
Fourth : session management
Read all Reaming step to our blog : Flask Login Web App FLASK BLOG APP DEVELOPEMENT

Top comments (0)