mercredi 24 juillet 2019

How can I fix this flask routing problem in my app?

I am setting up an app which is making a request to an API from a word input from the user on page A, then getting some data through a JSON output from the API and then displaying it on page B.

The problem is that I try to set up a route name "/bienvenue1" that Flask does not want, I get: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

only works fine with "/" as a route. But then, I don't manage to make my second post method page "/page ajoutée" because the url is still "/" though the render_template worked well...If I try to use the text input box on the bienvenue1.html page, it still tries to operate my def text_box() function and I get: werkzeug.exceptions.HTTPException.wrap..newcls: 400 Bad Request: KeyError: 'text'

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter
from flask_mail import Mail
from flask import render_template
from flask import request
from flask import jsonify
import requests 

app = Flask(__name__)

@app.route('/homee')
def index():
    return render_template("homee.html")

@app.route('/profile')
@login_required
def profile():
    return '<h1>This is the protected profile page!'

@app.route('/bienvenue1', methods=['POST'])
def text_box():
    text = request.form['text']
    # call the api
    API_KEY = "blablabla"
    url= "blablablabla".format(text)+API_KEY
    response = requests.get(url)
    result=response.json()
    result_processed=result['articles'][0:4]
    u=[]
    v=[]
    x=[]
    for i in result_processed:
        u.append(i['url'])
        v.append(i['urlToImage'])
        x.append(i['description'])
    return render_template("bienvenue1.html",lienimage0=v[0],lienimage1=v[1],lienimage2=v[2],message1=u[0],message2=u[1],message3=u[2],description0=x[0],description1=x[1],description2=x[2])

@app.route('/mesarticles')
def mesarticles():
    return render_template("mesarticlesenregistres.html")

@app.route('/pageajoutee', methods=['POST'])
def text_box1():
    text1 = request.form['text1']
    url=Two(id=1,url1='text1')
    db.session.add(url)
    db.session.commit()
    return 'article ajouté à votre liste'

if __name__ == '__main__':
    app.run(debug=True)

I've been turning in circles since days with that...If anyone could help would be great.

Thanks a lot

Paul




Aucun commentaire:

Enregistrer un commentaire