Flask - sending form return to another function

0

Below is my default.py where I define the end-points; What I'm trying to do is grab the FORM information that is in "/" send to return_request () and show the result of the 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')
    
asked by anonymous 08.05.2018 / 17:06

2 answers

0

I found the solution 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've combined everything into a single function ... it's still not the format I wanted, but it's working ...

    
09.05.2018 / 15:13
0

A space is missing in the 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 "everything", I personally prefer 3 or 4 spaces, like this:

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')
    
08.05.2018 / 17:11