Нужно проверить уникальность никнема при регистрации пользователей. Попробовал такой код, не работает. Какие есть предложения, товарищи?
from flask import render_template, flash, redirect, session, url_for, request, g
from app import app, db, models
from models import User, ROLE_USER, ROLE_ADMIN
from forms import RegisterForm
import flask
@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
def index():
form = RegisterForm()
ROLE_USER = 0
counter = 0
nick = ''
if form.validate_on_submit():
user = User(nickname = form.Nickname.data, email = form.Email.data, password = form.Password.data, role = ROLE_USER)
users = models.User.query.all()
for u in users:
nick = u.id,u.nickname
if nick == form.Nickname.data:
counter += 1
if counter == 0:
db.session.add(user)
db.session.commit()
return redirect('/')
else:
flash('ebat ti loh')
return render_template('index.html',
form = form,
counter = counter,
nick = nick)