I’m currently developing a flask web application in pycharm
. The app itself works properly but when I tried using MongoDB to store and collect user ids for "log in and register" purpose, it keeps throwing me this error- AttributeError: ‘NoneType’ object has no attribute ‘users
If someone can help me with this problem, it would be really helpful.
Here’s the code for my app.py
from flask import Flask, request, url_for, redirect, render_template, session, flash
import pickle
import numpy as np
from flask_pymongo import PyMongo
import bcrypt
from flask_session import Session
import smtplib
import warnings
import joblib
warnings.filterwarnings('ignore')
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
app.config['MONGO_DBNAME'] = 'Credentials'
app.config['MONGO_URI'] = "mongodb+srv://yashvir:(iusedmypass)@cluster0.fskb7t4.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
mongo = PyMongo(app)
mod = joblib.load('forestfiremodel.pkl')
model = pickle.load(open('model.pkl', 'rb'))
@app.route('/')
def index():
if 'username' in session:
return render_template("FrontEnd.html")
return render_template("index.html")
@app.route('/login', methods=['POST', 'GET'])
def login():
error = None
users = mongo.db.users
login_user = users.find_one({'name': request.form['username']})
if login_user:
if bcrypt.hashpw(request.form['pass'].encode('utf-8'), login_user['password']) == login_user['password']:
session['username'] = request.form['username']
return redirect(url_for('index'))
else:
error = 'Please Enter Credentials Correctly or Create New Username & Password!'
return render_template('index.html', error=error)
else:
error = 'Please Enter Credentials Correctly or Create New Username & Password!'
return render_template('index.html', error=error)
@app.route("/logout")
def logout():
# session.pop('username', None)
session["username"] = None
return render_template("index.html")
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'POST':
users = mongo.db.users
existing_user = users.find_one({'name': request.form['username']})
if existing_user is None:
hashpass = bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt())
users.insert({'name': request.form['username'], 'password': hashpass})
session['username'] = request.form['username']
warning = 'You Have Register Successfully.rn'
'Please Enter your Credentials again'
return render_template('index.html', warning=warning)
else:
flash(message='The username already exists!')
return render_template('register.html')
if __name__ == '__main__':
app.secret_key = 'secretkey'
app.run(debug=True)
I tried establishing connection with other apps like compass and vscode and both of them are able to connect properly but then the authorization doesn’t work.
The two collections "users" and "contactme" are present in the DB Credentials in mongoDB
I double checked the url, and it seems to be working fine. I don’t know where this problem is coming from.
2
Answers
I solved the issue. Basically Flask-PyMongo was causing connectivity issue, which is weird.
It worked when I used this:
You should execute your query by
.exec()