Skip to main content
 首页 » 编程设计

部署在 pythonanywhere 上的 Python Flask 应用提交时出错

2024年10月17日9虾米姐

我已经在 pythonanywhere 上部署了成功运行的 python flask 应用程序。 仅显示主页,但当我提交表单时抛出 Internal Server Error.

可能是什么情况。检查控制台网络,我使用的请求链接未被访问,并且出现错误。

我已经在 virtualenv 中为项目成功安装了 requests 和 BeautifulSoup 依赖。

这是代码

from flask import Flask,render_template 
from flask import request    
#lets do the scraping here 
import requests 
from bs4 import BeautifulSoup 
 
 
app=Flask(__name__) 
 
@app.route('/',methods=['GET','POST']) 
def index(): 
    if request.method== "GET" : 
        return render_template('home.html',result="") 
    else: 
        result=requests.post("jobkhulyo.com/") 
        soup1=BeautifulSoup(result.content) 
        return render_template('home.html',result=soup1); 
 
if __name__=="__main__": 
    app.run(debug=True) 

GET 方法运行良好,但对于 POST 方法,它抛出一个内部服务器错误。 Home.html 是模板目录下的模板文件。相同的模板在 POST 方法中加载,但在提交表单时具有一些值。

请您参考如下方法:

传递给请求的 URL 必须以方案开头,例如 http:// 或 https://。您的 URL 没有,因此它引发了一个 MissingScheme 异常,它准确地解释了问题是什么。

MissingSchema: Invalid URL 'jobkhulyo.com': No schema supplied. Perhaps you meant http://jobkhulyo.com? 

按照提示,向 http://jobkhulyo.com 提出请求。

当以 Debug模式在本地运行服务器时,Flask 将显示发生的错误的回溯。在生产中,它会显示 500 错误,但仍会记录回溯。启用日志记录以查看这些错误,如 Flask application traceback doesn't show up in server log 中所述.