From 8faabf97754a135d3028e81676c9413f44a27bbe Mon Sep 17 00:00:00 2001 From: Kyle Roux Date: Tue, 16 May 2017 10:51:21 -0700 Subject: [PATCH] added tmeplate macros and base template for forms --- app.py | 60 +++++++++++++++++++-------------- templates/layouts/master.html | 19 +++++++---- templates/web/_macros.html | 15 +++++++++ templates/web/contact_form.html | 12 +++++++ templates/web/edit_contact.html | 39 ++------------------- templates/web/new_contact.html | 21 ++---------- 6 files changed, 79 insertions(+), 87 deletions(-) create mode 100644 templates/web/_macros.html create mode 100644 templates/web/contact_form.html diff --git a/app.py b/app.py index 9ef1de2..3297a53 100644 --- a/app.py +++ b/app.py @@ -10,6 +10,11 @@ app.secret_key = 'my secret' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book.sqlite' # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root@localhost/book' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +@app.context_processor +def add_lang_to_templates(): + return {'lang': 'en'} + db.init_app(app) @@ -54,33 +59,38 @@ def edit_contact(id): Edit contact :param id: Id from contact - ''' - form = ContactForm() + ''' + rtn = None my_contact = Contact.query.filter_by(id=id).first() - if form.validate_on_submit(): - # Get form - name = form.name.data - surname = form.surname.data - email = form.email.data - phone = form.phone.data - - try: - # Update contact - my_contact.name = name - my_contact.surname = surname - my_contact.email = email - my_contact.phone = phone - db.session.add(my_contact) - db.session.commit() - # User info - flash('Saved successfully', 'success') - except: - db.session.rollback() - flash('Error update contact.', 'danger') - return render_template( + if request.method.lower() == 'get': + form = ContactForm(obj=my_contact) + else: + form = ContactForm() + if form.validate_on_submit(): + form.populate_obj(my_contact) + # Get form + name = form.name.data + surname = form.surname.data + email = form.email.data + phone = form.phone.data + try: + # Update contact + my_contact.name = name + my_contact.surname = surname + my_contact.email = email + my_contact.phone = phone + db.session.add(my_contact) + db.session.commit() + # User info + flash('Saved successfully', 'success') + rtn = redirect(url_for('index')) + except: + db.session.rollback() + flash('Error updating contact.', 'danger') + return rtn if rtn is not None else render_template( 'web/edit_contact.html', form=form, - my_contact=my_contact) + contact_id=my_contact.id) @app.route("/contacts") @@ -125,4 +135,4 @@ def contacts_delete(): if __name__ == "__main__": app.debug = True - app.run() + app.run(host='0.0.0.0', port=8888, debug=True) diff --git a/templates/layouts/master.html b/templates/layouts/master.html index dd97ee8..89cc1d3 100644 --- a/templates/layouts/master.html +++ b/templates/layouts/master.html @@ -1,12 +1,12 @@ - + - - {% block title %}{% endblock %} | Contacts Flask - - + + {% block title %}{% endblock %} | Contacts Flask + +