Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, request, render_template, redirect, url_for
- from models import *
- app = Flask(__name__)
- app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
- app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
- db.init_app(app)
- @app.route('/')
- def index():
- people = Person.query.all()
- return render_template('index.html', people=people)
- @app.route('/create', methods=['GET', 'POST'])
- def create():
- if request.method == 'POST':
- # Create a person
- name = request.form.get('name')
- email = request.form.get('email')
- person = Person(name=name, email=email)
- db.session.add(person)
- db.session.commit()
- return redirect(url_for('index'))
- return render_template('create.html')
- @app.route('/show/<int:id>')
- def show(id):
- person = Person.query.get(id)
- if person is None:
- return render_template('error.html', message='No person with that ID.')
- return render_template('show.html', person=person)
- @app.route('/edit/<int:id>', methods=['GET', 'POST'])
- def edit(id):
- person = Person.query.get(id)
- if person is None:
- return render_template('error.html', message='No person with that ID.')
- if request.method == 'POST':
- person.name = request.form.get('name')
- person.email = request.form.get('email')
- db.session.commit()
- return redirect(url_for('show', id=person.id))
- return render_template('edit.html', person=person)
- @app.route('/destroy', methods=['POST'])
- def destroy():
- id = request.form.get('id')
- person = Person.query.get_or_404(id)
- db.session.delete(person)
- db.session.commit()
- return redirect(url_for('index'))
- def main():
- db.create_all()
- if __name__ == '__main__':
- with app.app_context():
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement