[Flask] Flasks and forms query

Corey Boyle coreybrett at gmail.com
Mon Aug 5 08:02:02 EDT 2019


If you remove the IF block for validating, then anytime that page is
requested (not just form submissions) you will get new DB records created.
Is your sub-category model related to the category model? If so, this line "
SubCategories(subcategory=form.subcategoryfield.data)" needs to reflect
that.

On Mon, Aug 5, 2019 at 7:43 AM Sean Murphy <mhysnm1964 at gmail.com> wrote:

> Thanks, I’ve tried that and it didn’t work. I’m gonna remove the F
> statement for the validation and see if that makes a difference. As I think
> the validation is causing the issue.
>
> My experience is the part
>
> On 5 Aug 2019, at 9:21 pm, Corey Boyle <coreybrett at gmail.com> wrote:
>
> Perhaps this...
> SubCategories(subcategory=form.subcategory.data)
> should be this...
> SubCategories(subcategory=form.subcategoryfield.data)
>
>
> On Mon, Aug 5, 2019 at 6:25 AM <mhysnm1964 at gmail.com> wrote:
>
>> All,
>>
>>
>>
>> I am fairly new to Python and Flask. So there is a lot of concepts I will
>> not understand. So please bare with me if I don’t use the right terminology.
>>
>>
>>
>> The issue I am having is with flask_wtf and forms. I am using
>> SelectFields 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>
>>
>> … More HTML is here but not related to the question.
>>
>> {% 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
>>
>>
>> _______________________________________________
>> Flask mailing list
>> Flask at python.org
>> https://mail.python.org/mailman/listinfo/flask
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190805/6fe9f987/attachment-0001.html>


More information about the Flask mailing list