0

Hi there I'm creating a Flask web app and now I have to create more cruds, so I decided to modularize the app using Blueprints.

I have a Login function on main.py that allows me to enter the app interface:

app.route('/', methods=['GET', 'POST'])
def login():
    # Output message if something goes wrong...
    msg = ''
    # Check if "username" and "password" POST requests exist (user submitted form)
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']
        # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(
            'SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password,))
        # Fetch one record and return result
        account = cursor.fetchone()
        # If account exists in accounts table in out database
        if account:
            # Create session data, we can access this data in other routes
            session['loggedin'] = True
            session['id'] = account['id']
            session['username'] = account['username']
            # Redirect to home page
            return redirect(url_for('client.home'))
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    # Show the login form with message (if any)
    return render_template('index.html', msg=msg)

It redirects to this Blueprint:

from flask import Blueprint, render_template
from flask import render_template, request, redirect, url_for, session, flash
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
from extension import mysql

client = Blueprint('client', __name__,
                   static_folder="../static", template_folder="../templates")


@client.route('/')
def home():
    if 'loggedin' in session:
        cur = mysql.connection.cursor()
        cur.execute('SELECT * FROM cliente')
        data = cur.fetchall()
        # User is loggedin show them the home page
        return render_template('home.html', username=session['username'], cliente=data)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))

It works just fine, but with a condition. On my main.py I also have this:

@app.route('/home')
def home():
    pass

And this is the problem, I don't know why I should keep this route on my main.py because if I delete it my app crashes and throws me this error:

werkzeug.routing.BuildError werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'client.home' instead?

I have no idea why does this happens. Why should I keep this route? or What I'm doing wrong? Could you please give me a hand? I've trying to change the redirect to using multiple routes, but if I delete that /home route.. my app crashes anyway.

Cromewar
  • 155
  • 9
  • You'll have a call to `url_for('home')` somewhere in your code. Maybe in a template? Your error should give you somewhere to look. – PGHE May 05 '21 at 22:00
  • yep you were right, I was refering to Home on my Layout.html... since I changed it.. Problem solved. Ty – Cromewar May 05 '21 at 23:13

2 Answers2

1

in url_for look for the name of the function ie url_for('function_name', parameters)

so to avoid the crash better to change the name of main.py home function to something else.

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • Thanks, that indeed helps. but I still have to maintain that route, Why I could not just delete it? I mean, delete the route, including the home() function? I don't use it att all – Cromewar May 05 '21 at 21:19
  • if you are not using home function and route in main.py delete it – sahasrara62 May 06 '21 at 05:18
0

Solved: I had one ref to Home on other file: Layout.html. Just removed the ref and it's solved

Cromewar
  • 155
  • 9