您好 Flask 开发人员,
我正在使用 DateField 来指定我的帖子日期。现在我正在为我的模型、表单、视图和 jinja2 模板系统使用适当的说明符,但日期仍然无法验证。我从日期纠察队中选择了正确的日期,但消息出现在日期选择器的底部,因为“此字段是必填项”,但由于我已经选择了日期,因此看到此消息毫无意义。这是我的代码,可以让您知道问题所在。
我的模型
class Post(Base, modelx.EventX):
poster = ndb.StringProperty(indexed= True, required=True)
postbody = ndb.StringProperty(indexed=True, required=True)
start_date = ndb.DateProperty(indexed= True, required=True)
我的表格
class CreatePost(wtf.Form):
poster = wtf.TextField('Post', validators=[validators.Required()])
postbody= wtf.TextAreaField('postbody', validators=[validators.Required()])
sdate= wtf.html5.DateField('Date', default='',validators=[validators.Required()], format='%m/%d/%Y')
我的看法
@app.route('/poster/',methods=['POST','GET'])
def post_it():
form = CreatePost(request.form)
if form.validate_on_submit() and request.method=='POST':
posting = model.Post(
poster = form.poster.data,
postbody = form.postbody.data,
sdate= form.sdate.data
)
try:
posting.put()
flash("Poster has been populated")
return (redirect(url_for('post_it')))
except CapabilityDisabledError:
flash('Error Occured while posting')
return redirect(url_for('post_it'))
return render_template('poster.html', form=form)
我的海报.html
{% extends 'base.html' %}
{% import 'macro/error.html' as forms %}
{% block title %} Post Something | Eventus {% endblock %}
{% block style_block %}
<link href="{{ url_for('static', filename='font-awesome/css/font-awesome.css') }}" rel="stylesheet" />
<link href="{{ url_for('static', filename='bootstrap/css/social-buttons.css') }}" rel="stylesheet" />
<style type="text/css">
.background {
background-color: #F3F3F3;
}
p.error {
color: red;
}
</style>
{% endblock %}
{% block content %}
<form action="{{ url_for('post_it') }}" class="navbar-form form-inline" method="post" id="new_postform">
{{ form.csrf_token }}
<fieldset>
{{ form.hidden_tag() }}
{% from "macro/_formhelpers.html" import render_field %}
<div class="control-group">
<p>{{ form.poster.label }}</p>
<div class="controls">
<p>
{{ form.poster(id='post',class='form-control required ',placeholder="My Post", autocomplete=True , size=3,**{'data-bind': 'value: bodyte'})}}</p>
{% if form.poster.errors %}
<ul class="errors">
{% for error in form.confirm.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
<div class="control-group">
<p>{{ form.postbody.label }}<p>
<div class="controls">
<p>{{ form.postbody(id='postbody',class='form-control required ',placeholder="Post Description", autocomplete=True , size=3,**{'data-bind': 'value: bodyte'})}}</p>
</div>
</div>
<div class="control-group">
<div class="controls">
{{ form.sdate.label }}
{{ form.sdate(id="est", class="required", formatString="MM/DD/YYYY") }}
{% if form.sdate.errors %}
<ul class="errors">
{% for error in form.sdate.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
<div class="control-group">
<div class="controls">
<br><input type="submit" id="postit" name="postit" value"poster" class="btn btn-primary" /><br><br>
</div>
</div>
</fieldset>
</form>
<div class="controls">
<!-- <p>Post: <input data-bind="value: bodyte" class="form-control" name="posts" /></p> -->
</div>
<p>BodyText: <strong data-bind="text: bodytext" /></strong></p>
{% block tail_script %}
<script src="{{ url_for('static', filename='bootstrap/js/jquery.validate.js') }}"></script>
<script src="{{ url_for('static', filename='bootstrap/js/additional-methods.js') }}"></script>
<script type="text/javascript">
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.bodyte = ko.observable();
this.bodytext = ko.computed(function(){
return this.bodyte();
}, this);
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
$(document).ready(function () {
$('#new_postform').validate({
rules: {
post: {
minlength: 2,
required: true
}
},
highlight: function (element) {
$(element).parent().css('border-color','red');
$(element).text('Please type some post').closest('.control-group').removeClass('success').addClass('error');
},
success: function (element) {
element.text('OK!').addClass('valid')
.closest('.control-group').removeClass('error').addClass('success');
}
});
});
</script>
{% endblock %}
{% endblock %}
请让我知道我的帖子和帖子正文在哪里得到很好的验证,但我的名为“sdate”的日期没有得到验证,如果我需要为 form.sdate(...) 字段添加任何属性,请告诉我。
请让我知道我在哪里犯了错误,在从日期选择器右侧选择日期后阻止我的日期验证事件。