I am creating an app that does some analysis, given a user enters in some IDs into the form. For example, if a user types 12345, 23456
into the TextField form, the app will run some analysis on these IDs and then display the results. My problem is that currently, when the user clicks "Submit" and the data analysis completes, it always redirects the user to www.website.com/results
. I need to create unique url's like www.website.com/results/12345+23456
so that 1) I can have multiple users and 2) users can send this link to people to re-generate the analysis.
Now, there are some questions on StackOverflow that are similar to my question but they are not the same and did not help me. So first, let me show some code before discussing that.
I have a home page which contains the the form:
<div>
<form action="https://website.com/results/" class="form-inline" method="post">
<div class="form-group">
<label for="PubmedID">Pubmed ID(s)</label>
<input type="text" class="form-control" id="PubmedID" name="pmid" value="{{request.form.pmid}}">
</div>
<button type="submit" id= "myButton" class="btn btn-default" data-toggle="modal" data-target="#myModal">Submit</button>
</form>
</div>
As you can see, the value
for the form is request.form.pmid
. My Flask-Wtform for this is here:
class pmidForm(Form):
pmid = TextField('PubmedID')
Since the action
of this form points towards website.com/results
that triggers my Flask function to be called:
@app.route('/results/', methods=["POST"])
def results():
form = pmidForm()
try:
if request.method == 'POST':
#entry = request.form or request.data doesn't help me...
entry = form.pmid.data #This is the user input from the form!
# DO LOTS OF STUFF WITH THE ENTRY
return render_template('results.html')
except Exception as e:
return(str(e))
As you can see I am using POST
and form.pmid.data
to get the data from the textfield form.
Again, I don't want to just redirect to /results
, I'd like to expand on that. I tried to modify my form so that the form action
pointed to https://website.com/results/{{request.form.pmid}}/
and then update the results function to be
@app.route('/results/<form_stuff>', methods=["POST"])
def results(form_stuff):
But this never worked and would re-direct me to a 404 not found page. Which I believe makes sense because there is no form data in the action
when the HTML is first rendered anyway.
Now, the other post that mine is similar to is: Keeping forms data in url with flask, but it quite doesn't answer or solve my problem. For tthis post, the key point that people made was to use POST
(which I already do), and to obtain and return the data with return request.args['query']
. For me, I'm already processing the form data as I need to, and I have my return render_template()
exactly how I want it. I just need to add something to the results
URL so that it can be unique for whatever the user put into the form.
What do I need to add to my form in the html and to my Flask /results
function in order to have the form data added into the URL? Please let me know if there's any other information I can provide to make my problem more clear. I appreciate the help! Thanks