Skip to content

Instantly share code, notes, and snippets.

@CosminNechifor CosminNechifor/login.html
Last active Jun 17, 2019

Embed
What would you like to do?
# this goes under ./templates
{% block content %}
<h1>Sign In</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}
</p>
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
# rest server with a database
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///examen.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Data(db.Model):
__table_args__ = {'sqlite_autoincrement': True}
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
data = db.Column(db.String(80), default="some_default_data")
@property
def serialize(self):
return {
'id': int(self.id),
'data': str(self.data)
}
@app.route('/api/v1/data', methods=['POST'])
def add_user():
try:
content = request.json
user = Data(
data=content['data']
)
db.session.add(user)
db.session.commit()
return jsonify({
'id': user.id,
'data': user.data
}), 200
except Exception as e:
return jsonify({
'error': str(e)
}), 404
@app.route('/api/v1/data', methods=['GET'])
def get_users():
try:
data_list = Data.query.all()
return jsonify([u.serialize for u in data_list])
except Exception as e:
return jsonify({
'error': str(e)
}), 404
if __name__ == '__main__':
db.drop_all()
db.create_all()
app.run(debug=True)
from flask_wtf import FlaskForm
from flask import (
Flask,
render_template,
flash,
redirect,
)
from wtforms import (
StringField,
PasswordField,
BooleanField,
SubmitField
)
from wtforms.validators import DataRequired
import os
SECRET_KEY = os.urandom(32)
app = Flask(__name__, template_folder='templates')
app.config['SECRET_KEY'] = SECRET_KEY
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
@app.route('/index', methods=['GET'])
def index():
return 'index page'
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
flash('Login requested for user {}, remember_me={}'.format(
form.username.data, form.remember_me.data))
return redirect('/index')
return render_template('login.html', title='Login', form=form)
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.