Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i'm trying to create login/registration form using python flask(where the user able to register successfully and then login with the username and password he register

i'm trying to create login/registration form using python flask(where the user able to register successfully and then login with the username and password he register with , saved in sql database called users.db), this my code ! but this error i can't fix at all can you check my code if there solution for this error and can my code be run without any other erorrs !!!

..............this is the error message............

File "/home/ubuntu/.local/lib/python3.9/site-packages/mysql/connector/connection.py", line 126, in _add_default_conn_attrs os_ver = "-".join(platform.linux_distribution()[0:2]) # pylint: disable=W1505 AttributeError: module 'platform' has no attribute 'linux_distribution'

..........................this pip installation

pip install flask-bcrypt pip install PyMySQL pip install PyMySQL[ed25519] pip install mysql-connector pip install mysql-connector-python pip install Flask-MySQLdb pip3 install pysqlite3 pip install flask_mail pip install pymssql

pip install flask-mysqldb

..........................this is my code

from flask import Flask, render_template, request, flash, session, redirect,url_for

from tempfile import mkdtemp

import os

import SQL

from datetime import datetime

from flask_bcrypt import Bcrypt

import pymysql

import mysql.connector as sql_db

from functools import wraps

import MySQLdb.cursors

import MySQLdb

import re

import mysql.connector as mysql

app = Flask(__name__)

pymysql.install_as_MySQLdb()

bcrypt = Bcrypt(app)

app.secret_key = '@))@))'

# Configure session to use filesystem (instead of signed cookies)

app.config["SESSION_FILE_DIR"] = mkdtemp()

app.config["SESSION_PERMANENT"] = False

app.config["SESSION_TYPE"] = "filesystem"

Session(app)

db = SQL("sqlite:///user.db")

mydb = sql_db.connect(

host = "remotemysql.com",

user = "root",

password = "A0n0UVlU3O",

database = "user"

)

cursor=mydb.cursor()

@app.route('/')

def home():

if 'loggedin' in session:

return render_template('home.html', username=session['username'])

return redirect(url_for('login3'))

@app.route("/login3", methods=["GET", "POST"])

def login3():

msg =''

if request.method == 'POST' and 'username' in request.form and 'password' in request.form:

username = request.form['username']

password = request.form['password']

cursor.execute('SELECT * FROM users WHERE username = %s AND password = %s', (username, password))

account = cursor.fetchone()

if account:

session['loggedin'] = True

session['id'] = account['id']

session['username'] = account['username']

return render_template("home.html")

else:

msg = 'Incorrect username / password !'

return render_template('login3.html', msg = msg)

@app.route('/reg', methods=['GET', 'POST'])

def reg():

msg = ''

if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:

username = request.form['username']

password = request.form['password']

email = request.form['email']

cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)

cursor.execute('SELECT * FROM users WHERE username = %s', (username))

account = cursor.fetchone()

if account:

msg = 'Account already exists!'

elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):

msg = 'Invalid email address!'

elif not re.match(r'[A-Za-z0-9]+', username):

msg = 'Username must contain only characters and numbers!'

elif not username or not password or not email:

msg = 'Please fill out the form!'

else:

cursor.execute('INSERT INTO users VALUES (NULL, ?, ?, ?)', (username, password, email,))

mysql.connection.commit()

msg = 'You have successfully registered!'

return redirect(url_for('home'))

else:

msg = 'Please fill out the form!'

return render_template('reg.html', msg=msg)

if __name__ == '__main__':

app.run(debug=True)

.......................................................................important note ..........................

***NOTE *** that i try to pip MYSQL to use :

from flask_mysqldb import MySQL

app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = 'your password' app.config['MYSQL_DB'] = 'userlogin' mysql = MySQL(app)

but give me another error that can't import MYSQL

and one last things :

is there another user name to use rather than root ?!

because its give error as well !!!!

mydb = sql_db.connect(

host = "remotemysql.com",

user = "root",

password = "A0n0UVlU3O",

database = "user"

)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions