简体   繁体   中英

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. 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.

2.My advice is to use sqlalchemy it will be very easy to sort the errors while connecting to database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM