weixin_33721344 2019-04-23 13:43 采纳率: 0%
浏览 43

渲染模板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条回答 默认 最新

  • weixin_33735676 2019-04-24 07:41
    关注

    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 ?

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?