Найти - Пользователи
Полная версия: Flask и mongoengine сохранение формы
Начало » Web » Flask и mongoengine сохранение формы
1
andreiru
Здравствуйте!

Совсем недавно работаю с Flask и mongoengine !

Есть модель:
class User(db.Document):
    name = db.StringField(min_length=3, max_length=16, unique=True, required=True)
    email = db.EmailField(unique=True, required=True)
    password = db.StringField(min_length=8, max_length=16, required=True)
   ...

И представление:
# регистрация пользователя
@app.route('/signup', methods=['GET', 'POST'])
def view_user_signup():
    UserForm = model_form(User)
    form = UserForm(request.form)
    if form.validate_on_submit():
        form.save()
        auth.login(form.instance.email, form.instance.password)
        return redirect(url_for('view_index'))
    return render_template('user_signup.html', form=form)


При сохранении формы вылетает:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/andrey/ineedto/app.py", line 107, in view_user_signup
    form.save()
  File "/usr/local/lib/python2.7/dist-packages/flask_mongoengine/wtf/models.py", line 21, in save
    self.instance.save(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 291, in save
    raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: ineedto.user.$name_1  dup key: { : "andrey" })

Почему она появляется, ведь эта ошибка должна уйти в {{ form.name.errors }} ?
lorien
Возможно, ваша технология не умеет корректно обрабатывать входные данные uniq полей в монго документах. Посмотрите, в исходном коде функции validate_on_submit(), как проверяются данные для name-поля
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB