简体   繁体   English

编码新手:尝试运行具有数据库连接的 flask web 应用程序时出现内部服务器错误

[英]Coding newbie: internal server error when trying to run a flask web app with database connection

I'm working on a simple flask web application.我正在开发一个简单的 flask web 应用程序。 I've uploaded everything in order to execute the project on my webserver.我已经上传了所有内容,以便在我的网络服务器上执行该项目。 Unfortunately it won't work (Error: Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.) I've yet found anything that could help me.不幸的是它不起作用(错误:内部服务器错误服务器遇到内部错误并且无法完成您的请求。服务器过载或应用程序中有错误。)我还没有找到任何可以帮助的东西我。

This is my code这是我的代码

from flask import Flask, render_template, url_for, request,\
    redirect, g
from wtforms import Form, StringField, PasswordField, validators
import mysql.connector 
from werkzeug.security import generate_password_hash
from db.db_credentials import DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE  

class RegistrationForm(Form):
    username = StringField('Nutzernamen angeben', [validators.Length(min=4, max=50)])
    email = StringField('Email angeben', [validators.Length(min=6, max=255)])
    password = PasswordField('Password angeben',
            [validators.length(min=4, max=256), validators.DataRequired(), validators.EqualTo('confirmation', message='Passwörter müssen übereinstimmen')])
    confirmation = PasswordField('Passwort wiederholen')


app = Flask(__name__)

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


@app.route('/registration', methods=["GET", "POST"])
def register(): 
    form = RegistrationForm(request.form)
    username = form.username.data
    password = form.password.data
    email = form.email.data

    password = generate_password_hash(password)
    if request.method == "POST" and form.validate():
        assert isinstance(DB_DATABASE, tuple)
        g.con = mysql.connector.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD,
                                        database=DB_DATABASE)
        cursor = g.con.cursor
        cursor.execute("INSERT INTO konten (username, password, email) VALUES {%s}, {%s}, {%s} ",
                       username, password, email)
        return redirect(url_for("login"))
    return render_template("register.html", form=form)

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

@app.route('/')
def myfun():
  return render_template('index.html')
@app.route('/index')
def index():
   return render_template('registration.html')

1.first you have to return a template. 1.首先你必须返回一个模板。

2.My advice is to use sqlalchemy it will be very easy to sort the errors while connecting to database. 2.我的建议是使用 sqlalchemy 连接数据库时很容易对错误进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试运行Flask时出现内部服务器错误 - Internal Server Error when trying to run flask Azure Flask Web App上的内部服务器错误 - Internal Server Error on Azure Flask Web App 尝试运行Flask Web App时获取未定义的错误为null - Getting a null is not defined error when trying to run flask web app 运行 Flask 应用程序时出现内部服务器错误 - Internal Server Error when running Flask App Flask 尝试运行 python 脚本时出现内部服务器错误 - Internal server error with Flask trying to run python script 为什么我在运行 flask 应用程序时在 Postman 上收到 500 Internal Server Error - Why I receive 500 Internal Server Error on Postman when I run a flask app 内部服务器错误:尝试使用烧瓶邮件时 - Internal Server Error: when trying to use flask-mail 使用数据库连接将代码部署到Azure Web时,Flask应用程序无法呈现,但是在本地服务器上运行良好 - Flask app fails to render when deploying code to Azure Web with a database connection, but works fine from local server 进行请求或数据库时,Flask + apache内部服务器错误 - Flask + apache Internal Server Error when making request or database 在Flask Web服务器上获取内部服务器错误 - Getting an internal server error on flask web server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM