Hello Flask Developers,
我正在使用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)
我的poster.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(...)字段添加任何属性。
请告诉我在从datepicker权利中选择日期后,我犯了错误,导致我的日期无法验证事件。
答案 0 :(得分:1)
我遇到了与我的DateFields相同的问题所以我实际上做的是不使用DateField而是使用StringField而是将日期字符串转换为表单的validate方法中的datetime对象
class Example(FlaskForm):
name = StringField('name', validators=[DataRequired()])
dob = StringField('dob')
def validate(self):
today = datetime.datetime.strptime(str(self.dob.data), '%d-%m-%y').date()
self.dob.data = today
return True
您可以保持模板的方式相同,并使用日期选择器..
希望它对你有用..
答案 1 :(得分:0)
由于Jinja最终呈现HTML,无论哪里出现问题,我通常会直接使用html标签而不出任何问题。也许你可以尝试一下。在任何情况下,您作为一种变通方法或解决方案得到了什么?