- Building Serverless Python Web Services with Zappa
- Abdulwahid Abdulhaque Barguzar
- 227字
- 2021-07-16 18:16:19
Views
Flask implemented the views in a very flexible way, where you can define the routes along with them. Flask's generic views implementation is inspired by Django's generic views. We will look at a detailed description of method views in a further section, but here, we are going to use simple views.
The following is the views snippet.
File—auth/views.py:
from flask import render_template, redirect, url_for
from flask_login import login_user, login_required, logout_user
from app.auth import auth
from app.auth.forms import LoginForm, SignupForm
from app.auth.models import User
@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user_by_email = User.query.filter_by(email=form.email.data).first()
if user_by_email is not None and user_by_email.verify_password(form.password.data):
login_user(user_by_email)
return redirect(url_for('todo.list'))
return render_template('auth/login.html', form=form)
@auth.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
if not User.query.filter_by(email=form.email.data).scalar():
User(
email = form.email.data,
password = form.password.data
).save()
return redirect(url_for('auth.login'))
else:
form.errors['email'] = 'User already exists.'
return render_template('auth/signup.html', form=form)
return render_template('auth/signup.html', form=form)
@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('auth.login'))
Here, we created the /login, /signup, and /logout routes, which we invoke based on HTTP requests. We are rendering an empty form instance on the HTTP GET request and processing the data on the POST request by using the Flask-WTF method and the validate_on_submit() method. While rendering the template, we are passing the form instance and redirect based on required actions.
Let's have a look at the templates mechanism in the next section.
- 物聯(lián)網(wǎng)檢驗(yàn)檢測(cè)技術(shù)
- 通信簡(jiǎn)史:從信鴿到6G+
- 物聯(lián)網(wǎng)通信技術(shù)
- 智慧光網(wǎng)絡(luò):關(guān)鍵技術(shù)、應(yīng)用實(shí)踐和未來(lái)演進(jìn)
- Microservice Patterns and Best Practices
- Kong網(wǎng)關(guān):入門、實(shí)戰(zhàn)與進(jìn)階
- 新手易學(xué):新手學(xué)淘寶開店
- 通信十年:擁抱互聯(lián)網(wǎng)
- 網(wǎng)管工具使用與技巧大全
- AIoT應(yīng)用開發(fā)與實(shí)踐
- 設(shè)備監(jiān)控技術(shù)詳解
- 5G非正交多址接入技術(shù):理論、算法與實(shí)現(xiàn)
- 信息技術(shù)安全評(píng)估準(zhǔn)則:源流、方法與實(shí)踐
- 網(wǎng)絡(luò)互聯(lián)技術(shù)(理論篇)
- Hands-On Cloud:Native Microservices with Jakarta EE