Forums

noob routing issue?

I'm trying to get my site/page to work as follows.. (this might be conceptually wrong!)

  • user goes to my site..http://thedoordoofer.pythonanywhere.com/

  • flask_app.py runs, (hopefully the scripts identifies that it's being accessed from the root, runs some code and serves my main form (I could provide the form with the default code here, but decided to do it on the page with JavaScript)

  • the main form page for loads, identifies(JavaScript) that no parameter was added to the URL, and populates the code field with a default value.

*user on page enters a value in text box, hit submit.

  • again flask_app.py is run, this time it identifies it was ran from a POST from the form, it runs some code, and then should take the user back to the form page, but this time passing back the users chosen code, so it can populate the text field or other text.

The issue I'm having is that the Flask_app.py always seems to run the "@app.route('/') def my_form():" code, and asserts that the code should be the default code (in the lots of code it uses this value to create an stl to be displayed on the form page) The end result is my page always creates and displays a default key!

What I have.. Flask_app.py

@app.route('/')
def my_form():
    return render_template('form_01.html')
    defaultcode = "0132X" #entering default code
    # Lots of other code....
    return render_template("form_01.html")

@app.route('/', methods=['POST'])
def my_form_post():
    code = request.form['u']
    # Lots of other code....
        return redirect("http://thedoordoofer.pythonanywhere.com?code="+code)

And in my form:

<form method="POST">
            <p>Code</p>
            <input name="u">
            <input type="Submit">
           <input type="button" onclick="location.href='http://thedoordoofer.pythonanywhere.com';" value="Restore Generic" />
          <form action="http://thedoordoofer.pythonanywhere.com">
          <input type="submit" value="Go to Google" />
    </form>
</form>

I hope this make sense, if not let me know (rolls eyes), I really just want to get this one page up and running to let people use my gadget, I had no idea that getting the page up and running was going to be such an uphill battle (and I've still to work out how to do a download button!) Any assistance would be greatly appreciated... Free doofer if someone does it for me!! LOL

Thanks, PaulP

do you mean that the POST endpoint never gets called? I wonder if for the first endpoint you need to do something like methods=['GET']

Hi Conrad,

Thanks for the reply... I think a lot of my issues are from just jumping in and not really understanding. The truth is I don't really want 'to be a Python programmer... I just want one page... but it's turned into a can of worms :(

The POST (route?) gets ran, when expected, it's that the part above it also runs, I think this is because when the form serves it's POST command I use the python script uses to reload the form

return redirect("http://thedoordoofer.pythonanywhere.com?code="+processed_text)

to redirect back to my loading page. I think my issues are due to me trying to pass parameters back and forth between the script and form, and trying to get my form to load without it running the script again.

From a single press on the submit button My Access log looks like this:

[29/Mar/2020:20:23:43 +0000] "POST /?code=2Y HTTP/1.1" 302 301 "http://thedoordoofer.pythonanywhere.com/?code=2Y" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36" "82.45.36.128" response-time=0.018
[29/Mar/2020:20:23:43 +0000] "GET /?code=2Y HTTP/1.1" 200 1441 "http://thedoordoofer.pythonanywhere.com/?code=2Y" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36" "82.45.36.128" response-time=0.002

I have tried as you suggested adding the "methods=['GET']" code but same result. I have tried altering the path to the form in the script to "/pages/form_01.html" (and adding this "/pages/---->/home/thedoordoofer/mysite/templates" to my static file setup, but I get "Cant find page error" when I try this.

Running out of ideas, but don't want to give in :)

Thanks again, PaulP

ah yes, that redirect would do a GET on that redirect url.

If you want to take the ?code= part of the redirected url and use it in the GET, you should probably not have the first return render_template('form_01.html')

Thanks Conrad,

I Think i have resolved the issue by essentially RTFM!...

I had a look at the return render_template command and seen that the correct way to pass a variable with this is by using:

return render_template("form_01.html", code=processed_text)

Using this is for both GET and POST requests allows me to load my page and provide the correct default or user code.

I've still to tidy it up, but it's now acting a bit more as I wanted, I'm still having an issue where the STL in the viewer seems to update on time rather than any sort of refresh, but I think that might be an issue with the Java STL viewer.

Thanks again,

PaulP

ok, great! thanks for letting us know!