こんにちは開発者 私は Python Flask 、Knockout.js および Google App Engine を使用しています。現在、ポスターである基本的なプログラムを作成しています。ポスターを使用していくつかのものを投稿しています。しかし、Google App Engine Datastore はrequest.json['...']
form.validate_on_submit() もビューの if 条件にある場合から値を取得しないようです。
ここに私のFlask Viewコードがあります
@app.route('/post/new',methods=['POST','GET'])
def post_it():
form = CreatePost(request.form)
use_db = ndb.Key(model.User, current_user.name)
if **form.validate_on_submit()** and request.method=='POST':
posting = model.Post(
name = use_db,
poster = request.json['post'],
postbody = request.json['postbody'],
posturl = request.json['posturl'],
)
try:
posting.put()
flash("Poster has been populated", category='info')
return jsonify({ "post": request.json['post'],"postbody": request.json['postbody'], "posturl": request.json['posturl'] })
except CapabilityDisabledError:
flash('Error Occured while posting')
return redirect(url_for('post_it'))
return render_template('poster.html', form=form, use_db = use_db, post_db = post_db)
@app.route('/posts',methods=['GET'])
def all_posts():
post_db = model.Post.query()
return jsonify(post_db=post_db)
私の poster.html コード
<form id="new_postform" action="{{ url_for('post_it') }}" data-bind="submit: addPost" class="navbar-form form-inline" method="post" >
{{ form.csrf_token }}
<fieldset>
{{ form.hidden_tag() }}
<div class="control-group">
<p>Post</p>
<div class="controls">
<p>
{{ form.poster(id='post',class='form-control required ',placeholder="My Post", autocomplete=True , size=3,**{'data-bind': 'value: post'})}}</p>
{% if form.poster.errors %}
<ul class="errors">
{% for error in form.poster.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
<div class="control-group">
<p>postbody<p>
<div class="controls">
<p>{{ form.postbody(id='postbody',class='form-control required ',placeholder="Post Description", autocomplete=True , size=3, **{'data-bind': 'value: postbody'} )}}</p>
</div>
</div>
<div class="control-group">
<p>post url</p>
<div class="controls">
<p>
{{ form.posturl(id='posturl',class='form-control required ',placeholder="My Post", autocomplete=True , size=3, **{'data-bind': 'value: posturl'} )}}</p>
{% if form.posturl.errors %}
<ul class="errors">
{% for error in form.posturl.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>
<ul data-bind="foreach: posts, visible: posts().length > 0">
<p data-bind="text: post"></p>
<p data-bind="text: postbody"></p>
<p data-bind="text: posturl"></p>
</ul>
<div class="controls">
</div>
{% 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 Post(data) {
this.post = ko.observable(data.post);
this.postbody = ko.observable(data.postbody);
this.posturl = ko.observable(data.posturl);
}
function PostViewModel() {
var self = this;
self.posts = ko.observableArray([]);
self.post = ko.observable();
self.postbody = ko.observable();
self.posturl = ko.observable();
console.log(self.post)
self.addPost = function() {
self.save();
self.post("");
self.postbody("");
self.posturl("");
};
$.getJSON('/posts', function(postModels) {
var t = $.map(postModels.posts, function(item) {
return new Post(item);
});
self.posts(t);
});
self.save = function() {
return $.ajax({
url: '/post/new',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({
'post': self.post(),
'postbody': self.postbody(),
'posturl': self.posturl(),
}),
success: function(data) {
console.log("Pushing to post array");
self.posts.push(new Post({ post: data.post, postbody: data.postbody, posturl: data.posturl }));
return;
},
error: function() {
return console.log("Failed");
}
});
};
}
// Activates knockout.js
ko.applyBindings(new PostViewModel());
</script>
{% endblock %}
{% endblock %}
form.validate_on_submit() を request.json['....'] で実行する方法はありますか?どこが間違っているのかわかりません。