成功部署 python 后,Azure Web App 显示默认页面或应用程序错误

时间:2021-02-08 17:34:24

标签: python azure azure-sql-database azure-webapps azure-appservice

开发了一个 Python Flask Web 应用程序并连接到 Azure SQL 数据库。代码在本地运行成功。

但是,当部署到 azure(基于 Linux 的 Web 应用程序)中的 Web 应用程序时,我会得到默认页面或“应用程序错误:如果您是应用程序管理员,则可以访问诊断资源。”

不确定我哪里出错了。任何帮助深表感谢。

下面是代码。

    import textwrap
import pyodbc
from flask import Flask, render_template, request

app = Flask(__name__)

# DataBase Connections

driver = '{ODBC Driver 17 for SQL Server}' 
server_name ='sample-web'
database_name ='sample-web'
server = '{server_name}.database.windows.net,1433'.format(server_name=server_name)
username = "ramazdemo"
password = "Password123"
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database_name+';UID='+username+';PWD='+ password)

# Code

@app.route('/', methods = ['GET','POST'])
def index():
    
    if request.method == 'POST':
        userdetails = request.form
        name = userdetails['name']
        email = userdetails['email']
        crsr: pyodbc.Cursor = cnxn.cursor()
        crsr.execute("""INSERT INTO users (Name,Email) VALUES(?,?)""",(name,email))
        crsr.commit()
        crsr.close()
        return 'success'
    return render_template('index.html')

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

App Service in Azure

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

问题排查

查找问题最好的方法是查询日志。

  1. 检查 ODBC 驱动程序(或者您可以按照我在下面帖子中的回答重新安装驱动程序)。

    How to access ODBC Driver on Azure App service

  2. 以另一种方式重新部署您的网络应用程序。原因是为了排除当前工具存在导致发布失败而无法识别的错误或环境因素。

    Deploy your Flask app on Azure in 3 easy steps