我正在设置一个应用程序,它从用户在页面a上输入的单词向API发出请求,然后通过API的JSON输出获取一些数据,然后将其显示在页面B上
问题是我试图设置一个Flask不想要的路由名“/bienvenue1”,我得到: 找不到 在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试。在
只有“/”作为路由才可以正常工作。但是,我无法使我的第二个post method page“/page ajoutée”,因为url仍然是“/”尽管render峎u模板工作得很好……如果我尝试在bienvenue1.html页面上使用文本输入框,它仍然会尝试操作我的def text_box()函数,我得到: werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'文本'
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)
从那以后我就一直在转圈子…如果有人能帮忙那就太好了。在
非常感谢
保罗
好的,所以我将试着把我从问题中理解的东西分解成碎片。在
当您尝试访问路由时,
/bienvenue1
不工作这里的原因很简单,当您尝试访问它时,您需要一个
GET
请求,而此时您只接受该路由中的POST
请求。如果您同时需要GET
和POST
,则应将其传递给方法。另外请注意,由于您同时接收这两种方法,因此您可能需要确保根据您是加载页面还是提交表单而采取不同的操作。在无法提交表格。
根据您在评论中分享的内容,如果您要提交表单,请在下面列出:
^{pr2}$但是,您正在将
action
设置为.
。在这里,您应该写下表单应该发送到的链接,或者以方便烧瓶的方式:您还可以尝试传递
url
结尾处的内容,至少出于测试目的。因此,实际上您需要将表单提交到右边action
,这样就可以呈现另一个页面了。在在post
如果您想在表单提交后从
/bienvenue1
重定向到/mesarticles
,只需在text_box()
函数中添加:这同样适用于
text_box1()
。在PS如果您可以编辑您的问题,添加(1)确切的期望值是什么,并添加(2)您的
html
代码,这将有所帮助相关问题 更多 >
编程相关推荐