渲染模板FLASK

I have a problem with reload page, when I click the button wchich is used to filtrate data witch using ajax script. In condition IF not working render template . Below sample code

Python

def viewchallenges():
        categories = Categories.query.all()
        if request.method=='POST':
                 category_id=Categories.query.filter_by(category=request.form['name']).first()
                 check=Challenge.query.filter_by(categorie_id=category_id.id).order_by(Challenge.timestamp.desc()).all()
                 if  not check:
                     flash(_('not found'))
                 else:
                     page = request.args.get('page', 1, type=int)
                     posts = Challenge.query.filter_by(categorie_id=category_id.id).order_by(Challenge.timestamp.desc()).paginate(
                         page, current_app.config['POSTS_PER_PAGE'], False)
                     next_url = url_for('main.viewchallenges', page=posts.next_num) \
                         if posts.has_next else None
                     prev_url = url_for('main.viewchallenges', page=posts.prev_num) \
                         if posts.has_prev else None
                     return render_template('viewChallanges.html', title=_('View Challenge'), posts=posts.items, next_url=next_url, prev_url=prev_url,categories=categories)
        page = request.args.get('page', 1, type=int)
        posts =Challenge.query.order_by(Challenge.timestamp.desc()).paginate(
                page,current_app.config['POSTS_PER_PAGE'], False)
        next_url = url_for('main.viewchallenges', page=posts.next_num) \
        if posts.has_next else None
        prev_url = url_for('main.viewchallenges', page=posts.prev_num) \
        if posts.has_prev else None
        return render_template('viewChallanges.html', title=_('View Challenge'), posts=posts.items, next_url=next_url, prev_url=prev_url,categories=categories)

HTML

{% for item_category in categories %}
                  <button class="btn btn-secondary fby_category">{{item_category.category}}</button>
              {% endfor %}

AJAX

 $('.fby_category').click(function(e) {
    var url = "{{ url_for('main.viewchallenges') }}";
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: url,
        data: {
            'name': $(this).text()
        },
     });
   });

1个回答


您要呈现一个新模板。 这意味着用户将获得一个新的网页。 然后,您在这里不需要Javascript。 您可以创建多个表单</ strong> </ p>

 <代码> {类别中%的item_category的%}
    

              {{item_category.category}}“ />
{%endfor%}
</ form>
</ code> </ pre>

更简单,不是吗?</ p>
     </ div>

展开原文

原文

You want to render a new template. It means that the user is going to get a new webpage. Then you do not need Javascript here. You can just create multiple forms

{% for item_category in categories %}
    <form action="{{ url_for('main.viewchallenges') }}" method="post">
        <input type="submit" name="{{item_category.category}}<" value=" 
    {{item_category.category}}" />
{% endfor %}
</form>

Easier, isn't ?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐