Flask - sending form back to another function

Asked

Viewed 858 times

0

Below is my default.py where I set the end-point; What I’m trying to do is get the information from FORM that’s on "/" send to the return_request() and append the result of Request in "/output"

def return_request(endPoint):
 headerInfo = {
    'content-type': 'application/ld+json',
    'X-API-KEY': '3feff58c-5302-49da-803e-25eb0a34dce5'
 }

 url = "..../"

 res = requests.get(url + '%s' % (endPoint), headers=headerInfo)

 return res


@app.route("/", methods=["GET", "POST"])
def index():
 form = ReqForm()
 if form.validate_on_submit():

    form.reset()
 else:
    pass

 return render_template('index.html', req=form)


@app.route("/output", methods=["GET", "POST"])
def output():

return render_template('output.html')

2 answers

0

One space left at last return, this:

@app.route("/output", methods=["GET", "POST"])
def output():

return render_template('output.html')

Should be:

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

In Python indentation is "all", I personally prefer 3 or 4 spaces, so:

def return_request(endPoint):
    headerInfo = {
        'content-type': 'application/ld+json',
        'X-API-KEY': '3feff58c-5302-49da-803e-25eb0a34dce5'
    }

    url = "http://beta.pdvmundo.solutions/api/"

    res = requests.get(url + '%s' % (endPoint), headers=headerInfo)

    return res


@app.route("/", methods=["GET", "POST"])
def index():
    form = ReqForm()

    if form.validate_on_submit():
        form.reset()
    else:
        pass

    return render_template('index.html', req=form)


@app.route("/output", methods=["GET", "POST"])
def output():
    return render_template('output.html')
  • Yes, in my code is indented good. is that I’m getting used to using Stackoverflow

  • @Cristophergollmann understand, so what error occurs exactly? /output is displaying empty or 404?

  • then is that so, what I want to know is how I send the imput from FORM to the function return_request() and then how I get the return of that function and show in /output

0


I found the solution by doing so:

@app.route("/index", methods=["GET", "POST"])
def load():
form = ReqForm()
if request.method == 'POST':
    output = request.form['field']
    print(output)
    endPoint = output
    headerInfo = {
        'content-type': 'application/ld+json',
        'X-API-KEY': '3feff58c-5302-49da-803e-25eb0a34dce5'
    }

    url = "..."

    res = requests.get(url + '%s' % (endPoint), headers=headerInfo)
    print(res.status_code)
    if res.status_code == 200:
        texto = json.loads(res.content)
        texto = texto.get("hydra:member")

else:
    pass

return render_template('load.html', form=form, texto=texto)

I combined everything within a single function... is not yet the format I wanted, but is working...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.