Python Forum
NameError: name ‘app_ctrl’ is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name ‘app_ctrl’ is not defined
#1
Hello! I finished a python course and I'm working on a project but I have some problems.
The most urgent would be: after filling in all the fields in the register form and submitting I receive an error “NameError: name‘ app_ctrl ’is not defined”. How can I define this app_ctrl to recognize it?
The problem appeared after I created a main.py and _init.py file, when this main.py did not exist, my function worked, I could register accounts using postman.

I also attached 2 pictures with the structure of the application, templates.py, forms.py and routes.py I left them out I didn't put them in a View folder.
Please help me I'm stuck here for better it's been a week, I don't know how to change it to work.

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!')
I tried to change
from application.Controller.app_ctrl import AppController
in
from application.Controller import app_ctrl
and I get the following error
Error:
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
Or if I change in function
verify_user_id = app_ctrl.get_user(user_id)
in
verify_user_id = AppController.get_user(user_id)
Error:
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, c

Attached Files

Thumbnail(s)
       
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'pi' is not defined katebishop 2 1,455 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  I'm getting a NameError: ...not defined. vonArre 2 2,039 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,732 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 4,207 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 4,610 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 2,583 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 15,047 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 17,882 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 4,170 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 3,410 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020