NameError: name 'app_ctrl' is not defined

Salutare! Am terminat un curs de python si lucrez la un proiect dar am o nelamurire.
Dupa ce completez toate campurile din formularul register si dau submit primesc o eroare “NameError: name ‘app_ctrl’ is not defined”. Cum as putea defini acest app_ctrl ca sa il recunoasca?
Problema a aparut dupa ce am creat un fisier main.py si _init.py, cand era fisier principal acest routes.py imi mergea functia.

Am atasat si 2 poze cu structura aplicatiei, templates.py, forms.py si routes.py le-am lasat in afara nu le-am pus intr-un folder View.
Multumesc!

bp1

Routes.py

 from datetime import datetime
 from application import app
 from flask import jsonify, redirect, render_template, request, url_for
 from application.forms import RegisterForm
 from application.Controller.app_ctrl import AppController
 from application.Controller.currencies_ctrl import CurrenciesController
 from application.Controller.users import UsersController
 from application.Controller.users_accounts import UsersAccountsController
 from application.Controller.users_cards import UsersCardsController
 from application.Controller.users_credentials import UsersCredentialsController
 from application.Controller.users_deposits_ctrl import UsersDepositsController
 from application.Controller.users_transactions import UsersTransactionsController
 from application.Model.Repository.currencies import DBCurrenciesRepository
 from application.Model.Repository.users import DBUsersRepository
 from application.Model.Repository.users_accounts import DBUsersAccountsRepository
 from application.Model.Repository.users_cards import DBUsersCardsRepository
 from application.Model.Repository.users_credentials import DBUsersCredentialsRepository
 from application.Model.Repository.users_deposits import DBUsersDepositsRepository
 from application.Model.Repository.users_transactions import DBUsersTransactionsRepository
 
 @app.route('/api/v1/register', methods=['POST', 'GET'])
 def register():
     form = RegisterForm()
     if form.validate_on_submit():
         user_id = form.user_id.data
         verify_user_id = app_ctrl.get_user(user_id)
         print(user_id)
         if verify_user_id:
             return jsonify(message='That user id already exists'), 404 #409
         else:
             first_name = form.first_name.data
             last_name = form.last_name.data
             email = form.email.data
             address = form.address.data
             phone_number = form.phone_number.data
             date_of_birth = form.date_of_birth.data
             join_date = datetime.now()
 
             username = form.username.data
             verify_username = app_ctrl.get_username(username)
             print(verify_username)
             if verify_username:
                 return jsonify(message='That user id already exists'), 404
             else:
                 password = form.password.data
 
         app_ctrl.register_user(user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date, username, password)
         return redirect(url_for('index'))
     return render_template('register.html', title='register', form=form)
 
 if __name__ == '__main__':
     users_rep = DBUsersRepository()
     users_account_repo = DBUsersAccountsRepository()
     users_transactions_repo = DBUsersTransactionsRepository()
     currencies_repo = DBCurrenciesRepository()
     users_deposits_repo = DBUsersDepositsRepository()
     users_cards_repo = DBUsersCardsRepository()
     users_credentials_repo = DBUsersCredentialsRepository()
 
     users_ctrl = UsersController(users_rep)
     users_account_ctrl = UsersAccountsController(users_account_repo)
     users_transactions_ctrl = UsersTransactionsController(users_transactions_repo)
     currencies_ctrl = CurrenciesController(currencies_repo)
     users_deposits_ctrl = UsersDepositsController(users_deposits_repo)
     users_cards_ctrl = UsersCardsController(users_cards_repo)
     users_credentials_ctrl = UsersCredentialsController(users_credentials_repo)
 
     app_ctrl = AppController(users_ctrl, users_account_ctrl, users_transactions_ctrl,currencies_ctrl, users_deposits_ctrl, users_cards_ctrl, users_credentials_ctrl)

forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, DateTimeField
from wtforms.validators import DataRequired,Length, Email,EqualTo, ValidationError


class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=6, max=13)])
    password = StringField('Password', validators=[DataRequired(), Length(min=6, max=13)])
    submit = SubmitField('Login')

class RegisterForm(FlaskForm):
    user_id = StringField('CNP: ', validators=[DataRequired(), Length(min=13, max=13)])
    first_name = StringField('First name: ', validators=[DataRequired(), Length(min=5, max=30)])
    last_name = StringField('Last name: ', validators=[DataRequired(), Length(min=5, max=30)])
    email = StringField('Email: ', validators=[DataRequired(), Email()])
    address = StringField('Address: ', validators=[DataRequired(), Length(min=5, max=50)])
    phone_number = StringField('Phone number: ', validators=[DataRequired(), Length(min=10, max=10)])
    date_of_birth = StringField('Date of birth: ', validators=[DataRequired(), Length(min=5, max=30)])

    username = StringField('Username: ', validators=[DataRequired(), Length(min=6, max=13)])
    password = PasswordField('Password: ', validators=[DataRequired(), Length(min=6, max=13)])
    password = PasswordField('Confirm password: ', validators=[DataRequired(), Length(min=6, max=13), EqualTo('password')])

    submit = SubmitField('Register now!')

Poți pune întreg mesajul de eroare?

da, am pus mai jos

multumesc!

# NameError
NameError: name 'app_ctrl' is not defined
## Traceback *(most recent call last)*
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *2095*, in `__call__`
return self.wsgi_app(environ, start_response)
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *2080*, in `wsgi_app`
response = self.handle_exception(e)
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *2077*, in `wsgi_app`
response = self.full_dispatch_request()
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *1525*, in `full_dispatch_request`
rv = self.handle_user_exception(e)
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *1523*, in `full_dispatch_request`
rv = self.dispatch_request()
* #### File "C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py", line *1509*, in `dispatch_request`
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
* #### File "C:\Users\i_han\BankApp\application\routes.py", line *131*, in `register`
@app.route('/api/v1/register', methods=['POST', 'GET'])
def register():
form = RegisterForm()
if form.validate_on_submit():
user_id = form.user_id.data
verify_user_id = app_ctrl.get_user(user_id)
print(user_id)
if verify_user_id:
return jsonify(message='That user id already exists'), 404 #409
else:
first_name = form.first_name.data
> NameError: name 'app_ctrl' is not defined
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
* `dump()` shows all variables in the frame
* `dump(obj)` dumps all that's known about the object

Se schimbă eroarea dacă înlocuiești linia
from application.Controller.app_ctrl import AppController
cu
from application.Controller import app_ctrl

SAU

verify_user_id = app_ctrl.get_user(user_id)
cu
verify_user_id = AppController.get_user(user_id)
?

Tind să cred că nu sunt definite/folosite corect clasele și funcțiile importate.

Versiunea TLDR :slight_smile:

  1. daca schimb

from application.Controller.app_ctrl import AppController

cu

from application.Controller import app_ctrl

primesc eroare ca nu exista functia ‘get_user’ desi e creata, o sa atasez mai jos si codul din app_ctrl.py

AttributeError

AttributeError: module ‘application.Controller.app_ctrl’ has no attribute ‘get_user’

Traceback (most recent call last)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2095, in __call__

return self.wsgi_app(environ, start_response)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2080, in wsgi_app

response = self.handle_exception(e)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2077, in wsgi_app

response = self.full_dispatch_request()

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1525, in full_dispatch_request

rv = self.handle_user_exception(e)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1523, in full_dispatch_request

rv = self.dispatch_request()

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1509, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

  • File “C:\Users\i_han\BankApp\application\routes.py”, line 131, in register

@app.route(‘/api/v1/register’, methods=[‘POST’, ‘GET’])

def register():

form = RegisterForm()

if form.validate_on_submit():

user_id = form.user_id.data

verify_user_id = app_ctrl.get_user(user_id)

print(user_id)

if verify_user_id:

return jsonify(message=‘That user id already exists’), 404 #409

else:

first_name = form.first_name.data

AttributeError: module ‘application.Controller.app_ctrl’ has no attribute ‘get_user’

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the “Traceback” headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

  • dump() shows all variables in the frame
  • dump(obj) dumps all that’s known about the object
  1. daca schimb

verify_user_id = app_ctrl.get_user(user_id)

cu
`

verify_user_id = AppController.get_user(user_id)`

imi mai cere un parametru

TypeError

TypeError: get_user() missing 1 required positional argument: ‘user_id’

Traceback (most recent call last)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2095, in __call__

return self.wsgi_app(environ, start_response)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2080, in wsgi_app

response = self.handle_exception(e)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 2077, in wsgi_app

response = self.full_dispatch_request()

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1525, in full_dispatch_request

rv = self.handle_user_exception(e)

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1523, in full_dispatch_request

rv = self.dispatch_request()

  • File “C:\Users\i_han\BankApp\env\lib\site-packages\flask\app.py”, line 1509, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

  • File “C:\Users\i_han\BankApp\application\routes.py”, line 131, in register

@app.route(‘/api/v1/register’, methods=[‘POST’, ‘GET’])

def register():

form = RegisterForm()

if form.validate_on_submit():

user_id = form.user_id.data

verify_user_id = AppController.get_user(user_id)

print(user_id)

if verify_user_id:

return jsonify(message=‘That user id already exists’), 404 #409

else:

first_name = form.first_name.data

TypeError: get_user() missing 1 required positional argument: ‘user_id’

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the “Traceback” headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

  • dump() shows all variables in the frame
  • dump(obj) dumps all that’s known about the object

app_ctrl.py

from application.Controller.currencies_ctrl import CurrenciesController
from application.Controller.users import UsersController
from application.Controller.users_accounts import UsersAccountsController
from application.Controller.users_cards import UsersCardsController
from application.Controller.users_credentials import UsersCredentialsController
from application.Controller.users_deposits_ctrl import UsersDepositsController
from application.Controller.users_transactions import UsersTransactionsController
from application.Model.Repository.currencies import DBCurrenciesRepository
from application.Model.Repository.users import DBUsersRepository
from application.Model.Repository.users_accounts import DBUsersAccountsRepository
from application.Model.Repository.users_cards import DBUsersCardsRepository
from application.Model.Repository.users_credentials import DBUsersCredentialsRepository
from application.Model.Repository.users_deposits import DBUsersDepositsRepository
from application.Model.Repository.users_transactions import DBUsersTransactionsRepository

def register_user(self, user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date, username, password):
    self.users_ctrl.create_user(user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date) 
    self.users_credentials_ctrl(username, password)

def get_username(self, username):
    return self.user_credentials_ctrl.get_username(username)

def get_user(self, user_id):
    return users_ctrl.get_user(user_id)
    

# def exchange(self, user_id, amount, from_currency, to_currency):
# Check balance in from_currency
# if self.
# Remove from user balance(from_currency)
# Get latest exchange rate from ExchangeRates table and apply it to the amount
# Add to user balance(to_currency)

if name == ‘main’:
users_repo = DBUsersRepository()
users_account_repo = DBUsersAccountsRepository()
users_transactions_repo = DBUsersTransactionsRepository()
currencies_repo = DBCurrenciesRepository()
users_deposits_repo = DBUsersDepositsRepository()
users_cards_repo = DBUsersCardsRepository()
users_credentials_repo = DBUsersCredentialsRepository()

users_ctrl = UsersController(users_repo)
users_accounts_ctrl = UsersAccountsController(users_account_repo)
users_transactions_ctrl = UsersTransactionsController(users_transactions_repo)
currencies_ctrl = CurrenciesController(currencies_repo)
users_deposits_ctrl = UsersDepositsController(users_deposits_repo)
users_cards_ctrl = UsersCardsController(users_cards_repo)
users_credentials_ctrl = UsersCredentialsController(users_credentials_repo)

app_ctrl = AppController(users_ctrl, users_accounts_ctrl, users_transactions_ctrl, currencies_ctrl, users_deposits_ctrl, users_cards_ctrl, users_credentials_ctrl)