简体   繁体   English

在视图中创建JSON对象并以HTML形式传递

[英]Creating a JSON Object in views and passing it in HTML

views.py - Django 2.0.2 views.py-Django 2.0.2

def hotels(request):
    list_of_hotels = Hotel.objects.order_by('hotel_name')
    template = loader.get_template('myapp/hotels.html')

    data = {}
    for each in list_of_hotels:
        data[str(each.hotel_name)] = 'null'
    json_data = json.dumps(data)

    context = {
        'list_of_hotels': list_of_hotels,
        'json_data': json_data,
    }
    return HttpResponse(template.render(context, request))

hotels.html hotels.html

<div class="row">
  <div class="col s12">
    <div class="row">
      <div class="input-field col s12">
        <i class="material-icons prefix">search</i>
        <input type="text" id="autocomplete-input" class="autocomplete">
        <label for="autocomplete-input">Search</label>
      </div>
    </div>
  </div>
</div>

<script>
  var jsonObj = "{{json_data}}";
  $(document).ready(function(){
    $('input.autocomplete').autocomplete({
      data: jsonObj,
    });
  });
</script>

I'm trying to parse a JSON Object from Django views into a script located inside the corresponding HTML page. 我正在尝试将Django对象中的JSON对象解析为位于相应HTML页面内的脚本。 I tried everything, but it just doesn't seem to work. 我尝试了所有操作,但似乎不起作用。 I searched Stack Overflow for about an hour now, and based on all the answers came to these snippets of code, which still don't work. 我搜索了Stack Overflow大约一个小时,然后根据所有这些代码段的答案,这些代码段仍然不起作用。 Any leads on how to go about it would be highly appreciated! 任何有关如何进行的线索将不胜感激!

PS: In the script in hotels.html, the data takes in a JSON Object. PS:在hotels.html的脚本中,数据采用JSON对象。 For more details on the initialisation, refer to this . 有关初始化的详细信息,请参阅

PPS: I am an absolute noob in JavaScript/ jQuery. PPS:我绝对不是JavaScript / jQuery的菜鸟。

Found a solution to my own problem after a lot of tries. 经过多次尝试,找到了解决我自己问题的方法。 Thanks to anyone who tried to help me. 感谢任何试图帮助我的人。 :) :)

hotels.html hotels.html

<div class="row">
 <div class="col s12">
  <div class="row">
   <div class="input-field col s12">
    <i class="material-icons prefix">search</i>
    <input type="text" id="autocomplete-input" class="autocomplete">
    <label for="autocomplete-input">Search</label>
   </div>
  </div>
 </div>
</div>

<script type='text/javascript'>
  var jsonObj = {{ json_data|safe }};
  $(document).ready(function(){
    $('input.autocomplete').autocomplete({
      data: jsonObj,
    });
  });
</script>

views.py views.py

def hotels(request):

    list_of_hotels = Hotel.objects.order_by('hotel_name')
    template = loader.get_template('myapp/hotels.html')

    data = {}
    for each in list_of_hotels:
        data[str(each.hotel_name)] = None
    json_data = json.dumps(data)

    context = {
        'list_of_hotels': list_of_hotels,
        'json_data': json_data,
    }
    return HttpResponse(template.render(context, request))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM