在同一页烧瓶上显示结果 - python

我试图在同一页面上显示搜索结果。当用户搜索输入内容发布到路线仪表板并且该路线返回JSON对象时,如何在实现卡中显示这些结果?我正在尝试使用AJAX和jquery。

@app.route('/dash',methods=['GET','POST'])
@login_required
def dashboard():
    form=searchDashBoard(request.form)
    if current_user.is_authenticated:
        if request.method == 'POST' and form.validate_on_submit():
            user_input = form.wineName.data
            similar=a.getClosestMatch(user_input)[:9]
            if not similar:
                flash(' Wine Not found ')
            else:
                newList=[]
                for id in similar:
                    print(id)
                    info= a.getWineInfo(id)
                    r=requests.get('https://www.vivino.com/api/wines/'+str(id)+'/wine_page_information').json()
                    pic_url = r['wine_page_information']['vintage']['image']['location']
                    info['url']="https:"+str(pic_url)
                    newList.append(info)
                return jsonify(newList)
    return render_template('dashboard.html',name=current_user.username,form=form)

阿贾克斯

当单击searchBtn时,它会从路由/ dash获取数据,然后循环遍历并创建cards.so基本上我想输出类似于searchCard.html的内容,但与我使用seachinput和button的页面相同。

    $('#searchBtn').click,(function(){
  $.getJSON('/dash',function(obj){
    $.each(obj,function(key,value){
      $('ul').append('<li>'+value.id+'</li>')

    })

  })
}

searchcard.html

{% block content %}
<div class="container">
<div class="row">
{% for w in newList %}
  <ul class="collection">
      <li class="collection-item avatar">
        <img src={{ w['url'] }} style="max-height: 100px" alt="wine Image" class="circle">
        <span class="title">{{w['name']}}</span>
        <p><i class="material-icons">location_on</i> {{ w['region'] }}
        </p>
        <p> <i class="material-icons">attach_money</i> {{ w['price'] }}</p>
        <a href="#!" class="secondary-content"><i class="material-icons addBtn" wineID="{{ w['id'] }}" >add</i></a>
      </li>
</ul>
    {% endfor %}
  </div>
</div>

  {% endblock %}

参考方案

括号中出现了一些时髦的事情:

 $.ajax("{{url_for('save') "}}.done(function(reply){

更改为:

 $.ajax("{{ url_for('save') }}").done(function(reply){

不过,请在浏览器控制台中查找提示,我们在不知道确切错误发生的情况下不能做太多事情。

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

如何将Python字节字符串表示形式转换为字节? - python

我在文本文件中存储了许多Python字节对象,这些Python打印的内容类似于"b'\x80\x03}q\x00.'"如何将每个对象转换回字节对象?换句话说,我正在尝试找到一个执行convert("b'\x80\x03}q\x00.'") == b'\x80\x03}q…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

语法中的Python字符串 - python

看到一段使用'str in str in str'语法的代码很奇怪,例如:>>> 'test' in 'testtest' in 'testtesttest' True >>> 'test' in 'testtest' in…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…