1
\$\begingroup\$

I'm building a contact manager with Flask for a community group, and I'm working on the function to export contacts as a spreadsheet. I want to export either all contacts, only those who have been assigned to a ticket seller, only those assigned to a specific ticket seller, or those assigned to no ticket seller.

The form layout I want is something like this:

  • RadioButton: All Sellers
  • RadioButton: Select Seller
    • SelectField: Seller ID
  • RadioButton: No Seller Assigned

with a select field tucked in between two of the radio buttons. This requires that I render the radio buttons individually, rather than letting Jinja render them as a group. This is possible, but referencing each individual button for rendering has proven difficult.

Here's the Jinja code I've come up with:

{% for subfield in form.sellers_filter %}
 {% if subfield.id.endswith('0') %}
 <div class="form-group col-4">
 {{ subfield.label }} {{ subfield }}
 </div>
 {% endif %}
 {% if subfield.id.endswith('1') %}
 <div class="form-group col-4">
 {{ subfield.label }} {{ subfield }}
 {{ form.seller }}
 </div>
 {% endif %}
 {% if subfield.id.endswith('2') %}
 <div class="form.group col-4">
 {{ subfield.label }} {{ subfield }}
 </div>
 {% endif %}
{% endfor %}

I'm bothered, though, by the subfield.id.endswith() part, and I'm hoping someone can suggest a more elegant solution.

For reference, here is my form code:

def list_sellers():
 c1 = aliased(Contact)
 c2 = aliased(Contact)
 return db.session.query(c2).
 select_from(c1).join(c2, c2.id==c1.seller_id).
 filter(c1.seller_id != None)
 
class ExportForm(FlaskForm):
 filtered = BooleanField('Apply Filters')
 sellers_filter = RadioField(
 'Filter by Seller',
 choices=[
 ('all', 'All Sellers'),
 ('select', 'Select Seller'),
 ('none', 'No Seller')
 ],
 validators=[Optional()]
 )
 seller = QuerySelectField(
 'Seller',
 query_factory=list_sellers,
 allow_blank=False,
 validators=[Optional()],
 render_kw={'class': 'form-select'},
 )
 submit = SubmitField('Download')
asked Oct 13, 2021 at 17:08
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This might be too late, but you can do this using the radio button's label field as follows

{% if subfield.label.text == 'Select Seller' %}

As per this stackoverflow post

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
answered Sep 3, 2022 at 1:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.