Python / Django:无法逐行返回列表元素

时间:2015-08-20 12:10:50

标签: python django httpresponse

我正在尝试从json文件中过滤城市,并在Django应用程序的帮助下,从过滤后的列表中显示前五个城市。

但是当我尝试逐行返回HTTPResponse中的list元素时,它会以单行提供所有内容。

views.py文件

from django.http import HttpResponse
import re
import os
from django.conf import settings

def index(request):
    a= str(request.GET['city'])
    with open(os.path.join(settings.MEDIA_ROOT, 'all_city_info.json')) as f:
        data = f.read()

    lis=sorted(x[9:-1] for x in re.findall('"text": "'+re.escape(a)+'[\s\S]+?"', data))
    return HttpResponse('\n'.join([str(x) for x in lis[:5]]))

正在使用城市变量中的用户输入,如下所示: http://127.0.0.1:8000/api/?city=New

我得到的结果:

  

纽约米尔斯|明尼苏达州|美国纽约米尔斯|纽约|美国   纽约州|纽约州|美国纽约州|诺福克郡|英国

请告知,当我返回HTTPResponse时,我该怎么办才能让我的城市符合浏览器。

谢谢!

示例数据,如果有人想看看 -

  

{“City”:{“values”:[{“text”:“Abee | Alberta | Canada”,“state”:“AB”,   “id”:21774},{“text”:“Acadia Valley | Alberta | Canada”,“state”:“AB”,   “id”:21775},{“text”:“Acme | Alberta | Canada”,“state”:“AB”,“id”:   21776},{“text”:“艾尔德里|艾伯塔省|加拿大”,“州”:“AB”,“id”:   21777},{“text”:“Alderson | Alberta | Canada”,“state”:“AB”,“id”:   21778},{“text”:“Alix | Alberta | Canada”,“state”:“AB”,“id”:21779},   {“text”:“Alliance | Alberta | Canada”,“state”:“AB”,“id”:21780},   {“text”:“Andrew | Alberta | Canada”,“state”:“AB”,“id”:21781},   {“text”:“Ardmore | Alberta | Canada”,“state”:“AB”,“id”:21782},   {“text”:“Ardrossan | Alberta | Canada”,“state”:“AB”,“id”:21783},   {“text”:“Ashmont | Alberta | Canada”,“state”:“AB”,“id”:21784},   {“text”:“Athabasca | Alberta | Canada”,“state”:“AB”,“id”:21785},   {“text”:“Atikameg | Alberta | Canada”,“state”:“AB”,“id”:21786},   {“text”:“Atmore | Alberta | Canada”,“state”:“AB”,“id”:21787},   {“text”:“Avenir | Alberta | Canada”,“state”:“AB”,“id”:21788},   {“text”:“Balzac | Alberta | Canada”,“state”:“AB”,“id”:21789},   {“text”:“Banff | Alberta | Canada”,“state”:“AB”,“id”:21790},{“text”:   “Barons | Alberta | Canada”,“state”:“AB”,“id”:21791},{“text”:   “Barrhead | Alberta | Canada”,“state”:“AB”,“id”:21792},{“text”:   “Bashaw | Alberta | Canada”,“state”:“AB”,“id”:21793},{“text”:   “Bassano | Alberta | Canada”,“state”:“AB”,“id”:21794},{“text”:   “Beaumont | Alberta | Canada”,“state”:“AB”,“id”:21795},{“text”:   “Beaverlodge | Alberta | Canada”,“state”:“AB”,“id”:21796},{“text”:   “Beiseker | Alberta | Canada”,“state”:“AB”,“id”:21797},{“text”:   “Bellevue | Alberta | Canada”,“state”:“AB”,“id”:21798},{“text”:   “Bellis | Alberta | Canada”,“state”:“AB”,“id”:21799},{“text”:   “Benalto | Alberta | Canada”,“state”:“AB”,“id”:21800},{“text”:   “Bentley | Alberta | Canada”,“state”:“AB”,“id”:21801},{“text”:   “卑尔根|艾伯塔省|加拿大”,“州”:“AB”,“id”:21802},{“text”:   “Berwyn | Alberta | Canada”,“state”:“AB”,“id”:21803},{“text”:“Big   谷|艾伯塔省|加拿大“,”州“:”AB“,”id“:21804},{”text“:   “Bilby | Alberta | Canada”,“state”:“AB”,“id”:21805},{“text”:“Bittern   Lake | Alberta | Canada“,”state“:”AB“,”id“:21806},{”text“:”Black   钻石|艾伯塔省|加拿大“,”州“:”AB“,”id“:21807},{”text“:   “Blackfalds | Alberta | Canada”,“state”:“AB”,“id”:21808},{“text”:   “Blackie | Alberta | Canada”,“state”:“AB”,“id”:21809},{“text”:   “Blairmore | Alberta | Canada”,“state”:“AB”,“id”:21810},{“text”:   “Blue Ridge | Alberta | Canada”,“state”:“AB”,“id”:21811},{“text”:   “Bluesky | Alberta | Canada”,“state”:“AB”,“id”:21812},{“text”:   “布拉夫顿|艾伯塔省|加拿大”,“州”:“AB”,“id”:21813},{“text”:“Bon   雅阁|艾伯塔省|加拿大“,”州“:”AB“,”id“:21814},{”text“:   “Bonnyville | Alberta | Canada”,“state”:“AB”,“id”:21815},{“text”:   “鲍登|艾伯塔省|加拿大”,“州”:“AB”,“身份证”:21816},{“text”:“弓   岛|艾伯塔省|加拿大“,”州“:”AB“,”id“:21817},{”text“:   “Boyle | Alberta | Canada”,“state”:“AB”,“id”:21818},{“text”:   “Brampton | Alberta | Canada”,“state”:“AB”,“id”:21819}]}}

1 个答案:

答案 0 :(得分:1)

\n并不代表HTML回复中的任何内容。请使用<br>标记来分隔行。