Reputation: 1159
I'm building a simple web-app in Python Flask but when I am trying to submit the form, I am getting this error message -
"TypeError: an integer is required (got type str)"
from flask import Flask, render_template, request, flash, redirect, url_for, session, logging
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
app = Flask(__name__)
# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_PORT'] = '8889'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '123456'
app.config['MYSQL_DB'] = 'myflaskapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
#init MYSQL
mysql = MySQL(app)
Articles = Articles()
@app.route('/')
def index():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/articles')
def articles():
return render_template('articles.html', articles = Articles)
@app.route('/article/<string:id>')
def article(id):
return render_template('article.html', id = id)
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('New Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
# Create cursor
cur = mysql.connection.cursor()
# Execute query
cur.execute("INSERT INTO users(name, email, username, password) VALUES (%s, %s, %s, %s)", (name, email, username, password))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('You are now registered and can log in', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
if __name__ == '__main__':
app.secret_key = 'secret123'
app.run(debug=True)
Traceback (most recent call last)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Harsh\Desktop\Projects\py\myflaskapp\app.py", line 58, in register
cur = mysql.connection.cursor()
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask_mysqldb\__init__.py", line 94, in connection
ctx.mysql_db = self.connect
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask_mysqldb\__init__.py", line 81, in connect
return MySQLdb.connect(**kwargs)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Harsh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)
I am not sure why this is happening, can anyone guide me on this please. I looked at the other solutions on here, but they didn't really work for me
Upvotes: 0
Views: 3733
Reputation: 1264
In config parameters PORT must be an Integer you are here passing it in string
#Config MySQL
app.config['MYSQL_PORT'] = '8889' ##pass 8889 instead of '8889'
Upvotes: 5