[Tutor] Flask and flask_wtf issue -- I hpe someone can help.
mhysnm1964 at gmail.com
mhysnm1964 at gmail.com
Mon Aug 5 06:14:10 EDT 2019
All,
Thanks to the starting point from Allan's chapter on web ffframeworks. I
have now build quite a nice little app showing a bunch of records from a
sqlite database in tables. The issue I am having is with flask_wtf and
forms. I have a Selector HTML which I want to grab the selected item from
the list of options. I cannot get the data from the selector.
If there is five items and I select the 2nd option. I want the value related
to option 2 to be retrieved and updating a database. Below is the
information I hope will make things clear.
HTML template:
{% extends "base.html" %}
{% block content %}
<h1>New Sub Category</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
{{ form.categoriesfield.label }}
{{ form.categoriesfield(rows=10) }}
<p>
{{ form.subcategoryfield.label }}<br>
{{ form.subcategoryfield(size=64) }}<br>
{% for error in form.subcategoryfield.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
<h2>List of sub Categories</h2>
<div class="data">
{% if prev_url %}
<a href="{{ prev_url }}">Newer Records</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Next Records</a>
{% endif %}
<table id='records_table'><tbody>
<caption> Records </caption>
<tr>
<th scope='col'>ID</th>
<th scope='col'>Sub Category Name</th>
<th scope='col'>Category Name</th>
</tr>
{% for row in records %}
<tr>
<td>{{row.id }}</td>
<td>{{row.subcategory }}</td>
<td>{{row.categories.category }}</td>
</tr>
{% endfor %}
</tbody></table>
{% if prev_url %}
<a href="{{ prev_url }}">Newer Records</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Next Records</a>
{% endif %}
</div>
{% endblock %}
Now for the form.py class related to this form:
from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField, SubmitField, TextAreaField,
TextField, IntegerField, RadioField, SelectField, SelectMultipleField
from wtforms.validators import ValidationError, DataRequired, EqualTo,
Length
from app.models import * # all table model classes
class SubCategoryForm (FlaskForm):
categoriesfield = SelectField('Categories', choices= [(c.id, c.category)
for c in Categories.query.order_by('category')])
subcategoryfield = StringField('Sub Category Name')
submit = SubmitField('Create SubCategory')
Now for the routes.py function which the issue occurs. As you can tell
below, I am passing the category id and category text value to the
SelectField field to create the HTML code. The text value is being displayed
and I want the ID to be retrieved. Then I am planning to update the
SubCategory table with the category id to establish the relationship for a
new sub_category.
from datetime import datetime
from flask import render_template, flash, redirect, url_for, request
from app import app, db
from app.forms import CategoryForm, SubCategoryForm
from app.models import * # all table model classes
@app.route('/subcategories', methods=['GET', 'POST'])
def subcategories ():
tables = Accounts.query.all()
form = SubCategoryForm()
print (form.categoriesfield.data)
if form.validate_on_submit():
subcategory_value =
SubCategories(subcategory=form.subcategory.data)
db.session.add(subcategory_value)
db.session.commit()
flash('Congratulations, a sub category was added. {}
{}'.format(subcategory_value, form.categoriesfield.data))
return redirect(url_for('subcategories'))
page = request.args.get('page', 1, type=int)
records = SubCategories.query.order_by(SubCategories.id).paginate(page,
app.config['POSTS_PER_PAGE'], False)
next_url = url_for('subcategories', page=records.next_num) if
records.has_next else None
prev_url = url_for('subcategories', page=records.prev_num) if
records.has_prev else None
return render_template('subcategories.html', tables = tables,
title='Manage sub Categories', form=form, records = records.items, next_url
= next_url, prev_url = prev_url)
I cannot work how to get the category ID. If I use validate or not on any
fields, it doesn't produce anything in the flash. The page refreshes and
leave the content in the field. The database isn't being updated with the
new sub_category. All the database relationships are working when I manually
insert the data into the table via SQLIte3.
Any thoughts? Have I made myself clear enough?
Sean
More information about the Tutor
mailing list