Optimize generate forms edit contact

This commit is contained in:
Andros Fenollosa 2017-05-11 23:51:06 +02:00
parent ce763d6493
commit af745f5f3f
3 changed files with 18 additions and 33 deletions

12
app.py
View File

@ -55,8 +55,13 @@ def edit_contact(id):
:param id: Id from contact :param id: Id from contact
''' '''
form = ContactForm()
my_contact = Contact.query.filter_by(id=id).first() my_contact = Contact.query.filter_by(id=id).first()
form = ContactForm(
name=my_contact.name,
surname=my_contact.surname,
email=my_contact.email,
phone=my_contact.phone
)
if form.validate_on_submit(): if form.validate_on_submit():
# Get form # Get form
name = form.name.data name = form.name.data
@ -79,8 +84,7 @@ def edit_contact(id):
flash('Error update contact.', 'danger') flash('Error update contact.', 'danger')
return render_template( return render_template(
'web/edit_contact.html', 'web/edit_contact.html',
form=form, form=form)
my_contact=my_contact)
@app.route("/contacts") @app.route("/contacts")
@ -108,8 +112,6 @@ def search():
def contacts_delete(): def contacts_delete():
''' '''
Delete contact Delete contact
:param id: Id from contact
''' '''
try: try:
mi_contacto = Contact.query.filter_by(id=request.form['id']).first() mi_contacto = Contact.query.filter_by(id=request.form['id']).first()

BIN
book.sqlite Normal file

Binary file not shown.

View File

@ -4,34 +4,17 @@
<h1>Edit contact</h1> <h1>Edit contact</h1>
<form method="post"> <form method="post">
{{ form.csrf_token }} {{ form.csrf_token }}
<div class="form-group{%if form.name.errors %} has-error{% endif %}"> {% for field in form %}
{{ form.name.label }} {% if field.label.text != 'CSRF Token' %}
{{ form.name(class='form-control', value=my_contact.name) }} <div class="form-group{%if field.errors %} has-error{% endif %}">
{% for error in form.name.errors %} {{ field.label }}
{{ field(class='form-control') }}
{% for error in field.errors %}
<span class="help-block">{{ error }}</span> <span class="help-block">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div class="form-group{%if form.surname.errors %} has-error{% endif %}"> {% endif %}
{{ form.surname.label }}
{{ form.surname(class='form-control', value=my_contact.surname) }}
{% for error in form.surname.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %} {% endfor %}
</div>
<div class="form-group{%if form.email.errors %} has-error{% endif %}">
{{ form.email.label }}
{{ form.email(class='form-control', value=my_contact.email) }}
{% for error in form.email.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="form-group{%if form.phone.errors %} has-error{% endif %}">
{{ form.phone.label }}
{{ form.phone(class='form-control', value=my_contact.phone) }}
{% for error in form.phone.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<input type="submit" class="btn btn-success" value="Save"> <input type="submit" class="btn btn-success" value="Save">
</form> </form>
{% endblock %} {% endblock %}