0

开发人员您好,我正在使用 Python Flask、Knockout.js 和 Google App Engine。现在我正在制作海报的基本程序,我只是使用海报发布一些东西。但是 Google App Engine Datastore 似乎没有从request.json['...'] form.validate_on_submit() 也处于视图的 if 条件时获取值。

这是我的烧瓶视图代码

@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)

我的海报.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 %}

有没有办法用 request.json['....'] 运行 form.validate_on_submit() ,我无法弄清楚我哪里出错了。

4

0 回答 0