Совсем недавно работаю с 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 }} ?