1

私はフラスコとモンゴエンジンを使ってWTFフォームに取り組んできました。新しいユーザーとして HTML 側に登録しようとすると、このデバッグ メッセージが表示されます。

ValidationError: ValidationError (User:None) (フィールドは必須です: ['password'] は日付 "asd" を解析できません: ['created_at']) ここに画像の説明を入力

私は基本的なジャンゴの背景を持っています。私はフラスコが初めてで、それを理解する方法がわかりません。

モデル:

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

フォーム.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'

ビュー.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 %}

どんな助けでも大歓迎です...

4

1 に答える 1