Python/Django : Unable to return list elements line by line

标签 python django httpresponse

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

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

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

我得到的结果:

New York Mills|Minnesota|United States New York Mills|New York|United States New York|New York|United States New York|Norfolk|United Kingdom

请告知,当我返回 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": "Airdrie|Alberta|Canada", "state": "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": "Bergen|Alberta|Canada", "state": "AB", "id": 21802}, {"text": "Berwyn|Alberta|Canada", "state": "AB", "id": 21803}, {"text": "Big Valley|Alberta|Canada", "state": "AB", "id": 21804}, {"text": "Bilby|Alberta|Canada", "state": "AB", "id": 21805}, {"text": "Bittern Lake|Alberta|Canada", "state": "AB", "id": 21806}, {"text": "Black Diamond|Alberta|Canada", "state": "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": "Bluffton|Alberta|Canada", "state": "AB", "id": 21813}, {"text": "Bon Accord|Alberta|Canada", "state": "AB", "id": 21814}, {"text": "Bonnyville|Alberta|Canada", "state": "AB", "id": 21815}, {"text": "Bowden|Alberta|Canada", "state": "AB", "id": 21816}, {"text": "Bow Island|Alberta|Canada", "state": "AB", "id": 21817}, {"text": "Boyle|Alberta|Canada", "state": "AB", "id": 21818}, {"text": "Brampton|Alberta|Canada", "state": "AB", "id": 21819}]}}

最佳答案

\n在您的 HTML 中没有任何意义回复。使用<br>而是用标签分隔行。

关于Python/Django : Unable to return list elements line by line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32117932/

相关文章:

python - 如何使用 Django 中的设置

ajax 函数回调中的 jQuery xhr 对象属性

ruby - Zip(不是 gzip)Ruby HTTPResponse

python - bar3d 图的颜色图/颜色问题

python - ipython:暂时转到 shell,稍后返回当前 ipython session

django - 使用 native 序列化器 Django 将数据序列化为 json 格式

java - 为什么 HTTP 响应没有给我指定的数据范围?

python - 更改 python 对象的类(强制转换)

python - 在 Python 3 中转换为不同的位置符号

Django:为什么我的 CharField 没有得到 vTextField 类?