[Tutor] Flask and flask_wtf issue -- I hpe someone can help.

2019-08-05 Thread mhysnm1964
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 %}

New  Sub Category



{{ form.hidden_tag() }}

{{ form.categoriesfield.label }}

{{ form.categoriesfield(rows=10) }}



{{ form.subcategoryfield.label }}

{{ form.subcategoryfield(size=64) }}

{% for error in form.subcategoryfield.errors %}

[{{ error }}]

{% endfor %}



{{ form.submit() }}



List of sub Categories



{% if prev_url %}

Newer Records

{% endif %}

{% if next_url %}

Next Records

{% endif %}



 Records   



ID

Sub Category Name

Category Name



{% for row in records %}



{{row.id }}

{{row.subcategory }}

{{row.categories.category  }}



{% endfor %}



{% if prev_url %}

Newer Records

{% endif %}

{% if next_url %}

Next Records

{% endif %}



{% 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 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inserting long URL's into comments & docstrings?

2019-08-05 Thread Ben Finney
James Hartley  writes:

> This should be a slow ball pitch. Unfortunately, I haven't stumbled
> across a reasonable answer yet.

You can find the answer directly in PEP 8.

> On occasion, I put long URL's into comments/docstrings simply to document
> where I found specific information.

That's good practice, thank you for doing it!

> However, to be a good disciple of PEP8, anything which can't fit
> within 72 characters needs to be split across multiple lines.

To be a good disciple of PEP 8, follow it to completion. That includes
the section directly after the introduction
https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds>.

If a tool is rigidly enforcing some rules of PEP 8, but failing to let
you follow that section? That tool is foolish and needs to be ignored.

Follow PEP 8, and use your judgement.

-- 
 \  “An expert is a man who has made all the mistakes which can be |
  `\ made in a very narrow field.” —Niels Bohr |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor