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!
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!')