In a couple of the Flask web dev tutorials, you used the following code for querying the MySQL database for to find a specific username:
x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
However, when filling out the registration form, an error is produced that says "not all arguments converted during string formatting." A similar error is encountered when creating the Login form. From reading the YouTube comments, it seems that this is due to an update to WTForms making it so the TextField alias for StringField is depricated. People posted a variety of solutions to this problem including:
x = c.execute("SELECT * FROM users WHERE username = %s", (username,))
x = c.execute("SELECT * FROM users WHERE username='%s'"%(thart(username)))
x = c.execute("SELECT * FROM users WHERE username = (%s)", [thwart(request.form['username'])])
May I ask what you think is the best solution and why?
You must be logged in to post. Please login or register an account.
youre probably using MySQL-db package. which doesnt work with python3, you dont want it. you want to use PyMySQL - pip install pymysql. recommend using virtual environment command with python 3.5 = 'pyvenv venv' then pip install your packages there after activating with 'source venv/bin/activate. heres what your functions should look like for login and register system (same as in tutorial, but with pymysql and argon2 for pass hashing (need latest passlib1.7) - need to pip install argon2_cffi).
#dbconnect.py
import pymysql
def connection(): conn = pymysql.connect(host = "localhost", #unix_socket = "/var/run/mysqld/mysqld.sock", user = "root", passwd = "yoursqlpassword", db = "dbname") c = conn.cursor() return c, conn
#init.py imports and functions
from flask import Flask, render_template, flash, request, url_for, redirect, session, send_file, send_from_directory, jsonify, abort from .dbconnect import connection from flask_wtf import FlaskForm from wtforms import BooleanField, TextField, TextAreaField, PasswordField, SubmitField, validators from passlib.hash import argon2 from pymysql import escape_string as thwart from functools import wraps
class RegisterForm(FlaskForm): username = TextField('Username', [validators.Length(min=5, max=20)]) email = TextField('Email Address', [validators.Length(min=8, max=50), validators.Email()]) password = PasswordField('Password', [validators.Length(min=6, max=20, message='Password must be at least 6 characters'), validators.Required(), validators.EqualTo('confirm', message='Passwords must match')]) confirm = PasswordField('Confirm Password') accept_tos = BooleanField('By registering, I accept the <br> <a href="/termsofservice/"> Terms </a> and <a href="/privacy/"> Privacy </a> (12-1-16)', [validators.Required()]) #recaptcha = RecaptchaField()