我一直在用烧瓶和mongoengine制作WTF表格。当我尝试在html端注册为新用户时,会出现此调试消息。
ValidationError:ValidationError(User:None)(需要字段:['password']无法解析日期“asd”:['created_at'])
我有基本的django背景。我是新手,我不知道如何解决这个问题。
型号:
import datetime
from copylighter import db, app
from slugify import slugify
from flask_login import UserMixin
class User(db.Document, UserMixin):
created_at = db.DateTimeField(default=datetime.datetime.now)
name = db.StringField(max_length=30, required=True, unique=True, help_text="Your helptext here")
email = db.StringField(max_length=100, required=True, unique=True, help_text="Your helptext here")
active = db.BooleanField()
password = db.StringField(max_length=255, required=True, help_text="Your helptext here")
slug = db.StringField(help_text="Your helptext here")
roles = db.ListField(db.ReferenceField('Role'))
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
return super(User, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
forms.py
from flask_wtf import Form
from wtforms.fields import StringField, PasswordField, BooleanField, SubmitField, HiddenField
from wtforms import validators
from models import User
import datetime
class SignUpForm(Form):
created_at = HiddenField()
name = StringField(u'Username', [validators.Required()])
email = StringField(u'Email', [validators.Required()])
password = PasswordField(u'Password', [validators.Required()])
submit = SubmitField('Sign Up')
class Meta():
__model__ = 'User'
views.py
from flask import Flask, render_template, redirect, url_for, request, flash
from flask_login import login_required, login_user
from copylighter import db, app, login_manager
import datetime
from forms import LoginForm,SignUpForm
from slugify import slugify
from flask_login import UserMixin
from models import User
@app.route("/register", methods=['GET','POST'])
def register():
formS = SignUpForm()
#if formS.validate_on_submit():
#name = formS.name.data
#email = formS.email.data
#password = formS.password.data
if request.method == 'POST':
formS = SignUpForm()
formS.created_at.data = datetime.datetime.now()
if formS.validate() == False:
return render_template('register.html', form=formS)
if formS.validate_on_submit():
newuser = User(formS.name.data, formS.email.data, formS.password.data)
newuser.save()
return redirect(url_for('profile'))
return render_template("register.html", form=formS)
最后 的 register.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<form action="" method="post">
{{form.hidden_tag()}}
{{form.name.label}}
{{form.name}}
{% if form.name.errors %}
<span class="text-danger">Error</span>
{% endif %}
{{form.email.label}}
{{form.email}}
{% if form.email.errors %}
<span class="text-danger">Error</span>
{% endif %}
{{form.password.label}}
{{form.password}}
{% if form.password.errors %}
<span class="text-danger">Error</span>
{% endif %}
{{ form.submit }}
</form>
</div>
{% endblock content %}
任何帮助将不胜感激......
答案 0 :(得分:0)
问题基于views.py
if formS.validate_on_submit():
newuser = User(formS.name.data, formS.email.data, formS.password.data)
newuser.save()
save()函数不知道在哪里插入 form.blahblah.data 。我将所需的模型变量添加到newuser对象中,并与来自html表单的值匹配
以下是正确的示例:
newuser = User(name=formS.name.data, email=formS.email.data, password=formS.password.data, created_at=datetime.datetime.now())