简体   繁体   English

WTForms - 提交后 DateTimeLocalField 数据为 None

[英]WTForms - DateTimeLocalField data is None after submit

After form is submitted with a POST request, every Field data has its value, except DateTimeLocalField .使用POST请求提交表单POST ,每个Field数据都有其值,但DateTimeLocalField除外。 Accessing DateTimeLocalField's data value is a type of None .访问 DateTimeLocalField 的数据值是一种None

Form形式

class ArticleForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired()])
    category = SelectField(u'Category', choices=categories.choices)
    town = StringField('Town', validators=[DataRequired()])
    minimal_price = IntegerField('Minimal price')
    article_image = FileField('Article_image', validators=[FileRequired()])
    time_left = DateTimeLocalField('Time to end', validators=[InputRequired()],
                              format='%Y-%m-%d %H:%M:%S')
    description = TextAreaField('Description', validators=[DataRequired()])

Validation: ( tested with is_submitted , all work except for article_form.time_left.data which is None )验证:(使用is_submitted测试,除article_form.time_left.dataNone之外的所有工作

if article_form.validate_on_submit():

    new_article = Article(name=article_form.name.data,
                          category=article_form.category.data,
                          town=article_form.town.data,
                          minimal_price=article_form.minimal_price.data,
                          article_image=name,
                          time_left=article_form.time_left.data, # <-- None
                          description=article_form.description.data,
                          user_id=current_user.id)

Any help to get data from DateTimeLocalField ?DateTimeLocalField获取数据有什么帮助吗?

Try changing the format of the DateTimeLocalField from尝试更改DateTimeLocalField的格式

format='%Y-%m-%d %H:%M:%S' 

to:到:

format='%Y-%m-%dT%H:%M'

Tip: you can print the actual content of the input field prior to the validation to confirm the correct formatting of the DateTimeLocalField field.提示:您可以在验证之前打印输入字段的实际内容,以确认DateTimeLocalField字段的正确格式。

Use wtforms.fields.html5.DateTimeLocalField instead of wtforms.DateTimeLocalField .使用wtforms.fields.html5.DateTimeLocalField而不是wtforms.DateTimeLocalField Set the format with date and time separated by a 'T'.设置日期和时间以“T”分隔的格式。 If you would want the current time as the default value, set default parameter.如果您希望当前时间作为默认值,请设置默认参数。

from wtforms.fields.html5 import DateTimeLocalField

class InterviewForm(Form):
    posted = DateTimeLocalField('Posted:', default=datetime.today, format='%Y-%m-%dT%H:%M')

I did extensive research on the same problem, this is a hack but I still got the timestamp from the tag which looked like:我对同一问题进行了广泛的研究,这是一个黑客,但我仍然从标签中获得了时间戳,看起来像:

<input id="time_left" name="time_left" required type="datetime-local" value="2018-11-15T04:44">

You basically search for the timestamp from the tag returned by the tag您基本上是从标签返回的标签中搜索时间戳

date = re.search('(\d{4})[/.-](\d{2})[/.-](\d{2})[T](\d{2})[:](\d{2})',
          str(form.time_left)).group())

Let me know if the solution worked for you or if found a better solution to the problem.让我知道该解决方案是否适合您,或者是否找到了更好的问题解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM