0

I am building an app that will display a year_from and year_to dropdown on my home/index page, and will return matching results on another page.

Desired Flow: The user is presented with 2 dropdowns with pre-populated data. User selects a year range on index route. That year range will then query the database and return all results that are between the year range on listings route.

I am able to get to the point where I can query my DB for year to and year from, and present them in a flask-wtf query select field.

Problem: I am not able to pick up on the user selected fields from the index page and use them to generate a query and return the results in my listing page. User selected fields are not being passed.

What am I missing?

Routes

from flask import render_template, flash, redirect, url_for, request
from app import app
from app.models import Listingyear
from app.forms import YearForm

@app.route('/', methods=['GET', 'POST'])
@app.route('/index')
def index():
    form = YearForm()
    if form.validate_on_submit():
        year_from = request.form.get(year_from_select)
        year_to = request.form.get(year_to_select)
        return redirect (url_for('listing'))
    return render_template('index.html', form=form) 

@app.route('/listing', methods=['GET', 'POST'])
def listing():
    return render_template('listing.html')

Forms

from flask_wtf import FlaskForm
from wtforms import SubmitField, SelectField
from wtforms.validators import DataRequired
from wtforms_alchemy.fields import QuerySelectField
from app.models import Listingyear

def Listingyear_from_query(): #for query_factory, sort year ascending, will update later
    return Listingyear.query

def Listingyear_to_query(): #for query_factory, sort year descending, will update later
    return Listingyear.query

class YearForm(FlaskForm):
    year_from_select = QuerySelectField(query_factory=Listingyear_from_query, allow_blank=False, get_label='year', validators=[DataRequired()])
    year_to_select = QuerySelectField(query_factory=Listingyear_to_query, allow_blank=False, get_label='year', validators=[DataRequired()])
    submit = SubmitField('Search')

Index

{% extends "base.html" %}

{% block content %}
<h1>My App</h1>
<form action="{{ url_for('listing') }}" method="post">
    {{ form.hidden_tag() }}
    {{ form.year_from_select }}
    {{ form.year_to_select }}
    <p>{{ form.submit() }}</p>
</form>
{% endblock %}
ramrod
  • 3
  • 1
  • rename `_form = YearForm()` to `_form = YearForm()` and also see on sbmit what request data you are getting – sahasrara62 Mar 09 '20 at 02:23
  • Does this answer your question? [Make a POST request while redirecting in flask](https://stackoverflow.com/questions/15473626/make-a-post-request-while-redirecting-in-flask) – pjcunningham Mar 09 '20 at 10:55
  • this solved my question - https://stackoverflow.com/a/42708439/12472958 – ramrod Mar 10 '20 at 05:11

1 Answers1

0

https://stackoverflow.com/a/42708439/12472958

This solves the question in that it shows simply how to pick up a value entered from a user and pass it to another route.

ramrod
  • 3
  • 1