Forums

Something went wrong mysql database WSGI

Hi guys my first version of web app worked but when I added some additionals features it keeps crashing. In erorr logs i get something like WSGI error mysql connector. I

2023-01-15 15:04:39,658: Error running WSGI application
2023-01-15 15:04:39,691: _mysql_connector.MySQLInterfaceError: Python type EmailField cannot be converted
2023-01-15 15:04:39,691:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__
2023-01-15 15:04:39,691:     return self.wsgi_app(environ, start_response)
2023-01-15 15:04:39,691: 
2023-01-15 15:04:39,691:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app
2023-01-15 15:04:39,692:     response = self.handle_exception(e)
2023-01-15 15:04:39,692: 
2023-01-15 15:04:39,692:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
2023-01-15 15:04:39,692:     response = self.full_dispatch_request()
2023-01-15 15:04:39,692: 
2023-01-15 15:04:39,692:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
2023-01-15 15:04:39,692:     rv = self.handle_user_exception(e)
2023-01-15 15:04:39,692: 
2023-01-15 15:04:39,692:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
2023-01-15 15:04:39,693:     rv = self.dispatch_request()
2023-01-15 15:04:39,693: 
2023-01-15 15:04:39,693:   File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
2023-01-15 15:04:39,693:     return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2023-01-15 15:04:39,693: 
2023-01-15 15:04:39,693:   File "/home/Klindworth99/mysite/application/routes.py", line 96, in sign_up
2023-01-15 15:04:39,693:     if form.validate_on_submit():

Can you guys help me? Thanks!

From the error message "Python type EmailField cannot be converted", it looks like you're trying to place a Python object (EmailField) directly into the database. It's a big generalisation, but databases can generally only store strings and numbers, so you need to ensure that those are the only things that you try to write to your database.

Thanks for reply, but on my localhost everything works fine! Here is the form class:

from flask_wtf import FlaskForm
from wtforms import StringField, SelectField, IntegerField, SubmitField, EmailField, FileField, PasswordField
from wtforms.validators import DataRequired, InputRequired, Length # Input required is for uploads
from application.models import User

class RegisterForm(FlaskForm):

    username = EmailField('Name', validators=[DataRequired(), InputRequired(), Length(min=3, max=35)], render_kw={"placeholder":"@Email Address"})
    password = PasswordField('Email',validators=[DataRequired(), InputRequired(), Length(min=8, max=35)], render_kw={"placeholder":"Password"})

    submit = SubmitField("Submit") #BUTTON OF SUBMIT

    #VERIFIY IF THE USERNAME EXIST!
    def validate_username(self, username_x):
        existing_username = User.query.filter_by(username = username_x).first() # VERIFY FOR USERNAME
        # IF THEY FIND ONE THEY WILL GET AN ERROR
        if existing_username:
            return True


class LoginForm(FlaskForm):

    username = EmailField('Name', validators=[DataRequired(), InputRequired(), Length(min=3, max=35)], render_kw={"placeholder":"@Email Address"})
    password = PasswordField('Email',validators=[DataRequired(), InputRequired(), Length(min=8, max=35)], render_kw={"placeholder":"Password"})

    submit = SubmitField("Submit") #BUTTON OF SUBMIT


class UploadFileForm(FlaskForm):
    file = FileField("File")
    company_name = SelectField('Company Name', choices=[])
    submit = SubmitField("Upload File", validators=[DataRequired(), InputRequired()], )

And my models.py:

#THIS IS FOR THE @@@@@@@@@@@@@@@@@@----DATABASE!!!!!!!!!!!!!!!
from application import app
from datetime import datetime
from application import db
from flask_login import UserMixin, LoginManager, login_user, logout_user,current_user


login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
login_manager.session_protection = "strong"


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100))
    password = db.Column(db.String(100))

    def __integer__(self):
        return self.id

Do i need to do anything else? Thanks!

I don't see the code that raised the exception in your last message.

I guess this is it? EmailField...it's odd...

class RegisterForm(FlaskForm):

    username = EmailField('Name', validators=[DataRequired(), InputRequired(), Length(min=3, max=35)], render_kw={"placeholder":"@Email Address"})
    password = PasswordField('Email',validators=[DataRequired(), InputRequired(), Length(min=8, max=35)], render_kw={"placeholder":"Password"})


    submit = SubmitField("Submit") #BUTTON OF SUBMIT

    #VERIFIY IF THE USERNAME EXIST!
    def validate_username(self, username_x):
        existing_username = User.query.filter_by(username = username_x).first() # VERIFY FOR USERNAME
        # IF THEY FIND ONE THEY WILL GET AN ERROR
        if existing_username:
                return True

Sorry for the second message i wrote it in hurry, what do i need to provide you for?

This is from the route.py:

@app.route("/sign_up", methods=['GET', 'POST'])
def sign_up(): 
    form = RegisterForm()

    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data)
        new_user = User(username = form.username.data, password=hashed_password)

        if form.validate_username(form.username.data) != True:
            db.session.add(new_user)
            db.session.commit()
            flash("Registration Successful!", "success")
            return redirect(url_for('login_in'))
        else:
            flash("This email address is already in use!", "error")



    return render_template("sign_up.html", title = 'Sign Up', form = form)

I have a really good idea for an accounting project and if everything works well i want to make my first business...and i want to buy an bigger plan but i need to know i can do it till the end, sorry for the problems! Thanks a lot for patience!

I found the problem:

def validate_username(self, username_x):
    existing_username = User.query.filter_by(username = username_x).first() # VERIFY FOR USERNAME
    # IF THEY FIND ONE THEY WILL GET AN ERROR
    if existing_username:
        return True

I don't know why i get the error for this...in the local host it works but here it doesn't... if you guys can help me with a tip, if not i will try to make it works, sorry for the two accounts i thought it's a problem from an wrong pip or something i will delete it the second account, thanks guys!

What's this code doing: new_user = User(username = form.username.data, password=hashed_password) -- isn't it creating a new user before the validation?