限制python请求的响应长度

限制python请求的响应长度,python,django,if-statement,string-length,Python,Django,If Statement,String Length,我正在设置一个视图,通过单击按钮将API中的数据保存到我的数据库中,我很难找出如何通过以下方式限制产品描述请求响应的大小: 如果描述的长度大于2000,请删除它末尾的一些字母,直到它达到2000的限制,但不要从请求中完全删除它 到目前为止,我所能做到的是,如果长度高于2000,则完全删除产品信息,如下所示 My django视图功能: def api_data(request): if request.GET.get('mybtn'): # to improve, == 'someth

我正在设置一个视图,通过单击按钮将API中的数据保存到我的数据库中,我很难找出如何通过以下方式限制产品描述请求响应的大小:

如果描述的长度大于2000,请删除它末尾的一些字母,直到它达到2000的限制,但不要从请求中完全删除它

到目前为止,我所能做到的是,如果长度高于2000,则完全删除产品信息,如下所示

My django视图功能:

def api_data(request):
    if request.GET.get('mybtn'):  # to improve, == 'something':
        resp_1 = requests.get(
            "https://www.test-headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=PARIS&limit=5000&currencyCode=CAD",
            headers={
                "Headout-Auth": HEADOUT_TEST_API_KEY
            })
        resp_1_data = resp_1.json()
        base_url_2 = "https://www.test-headout.com/api/public/v1/product/get/"

        for item in resp_1_data['items']:
            # concat ID to the URL string
            url = '{}{}'.format(base_url_2, item['id'] + '?language=fr')

            # make the HTTP request
            resp_2 = requests.get(
                url,
                headers={
                    "Headout-Auth": HEADOUT_TEST_API_KEY
                })
            resp_2_data = resp_2.json()

            if len(resp_2_data['contentListHtml'][0]['html']) < 2000: #represent the description of a product
                Product.objects.get_or_create(
                    title=item['name'],
                    destination=item['city']['name'],
                    description=resp_2_data['contentListHtml'][0]['html'],
                    link=item['canonicalUrl'],
                    image=item['image']['url']
                )

    return render(request, "form.html")
def api_数据(请求):
如果request.GET.GET('mybtn'):#要改进,=='something':
resp_1=requests.get(
"https://www.test-headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=PARIS&limit=5000¤cyCode=CAD",
标题={
“Headout Auth”:Headout\u TEST\u API\u密钥
})
resp_1_data=resp_1.json()
基本url_2=”https://www.test-headout.com/api/public/v1/product/get/"
对于resp_1_数据中的项目['items']:
#将concat ID添加到URL字符串
url='{}{}'。格式(基url_2,项['id']+'?语言=fr')
#发出HTTP请求
resp_2=requests.get(
网址,
标题={
“Headout Auth”:Headout\u TEST\u API\u密钥
})
resp_2_data=resp_2.json()
如果len(resp_2_data['contentListHtml'][0]['html'])<2000:#表示产品的描述
Product.objects.get_或_create(
标题=项目['name'],
目的地=项目['city']['name'],
description=resp_2_数据['contentListHtml'][0]['html'],
link=item['canonicalUrl'],
image=项目['image']['url']
)
返回呈现(请求“form.html”)
但我通过这样做删除了很多行,所以我想知道如何修复这个问题


请提供帮助。

您可以使用切片运算符指定描述的字符限制,而不是使用条件语句。使用这种方法,您可以重构代码的相关部分:

resp_2_data=resp_2.json()
Product.objects.get_或_create(title=item['name'],
目的地=项目['city']['name'],
description=resp_2_data['contentListHtml'][0]['html'][0:2000],
....
)

您可以使用切片运算符指定描述的字符限制,而不是使用条件语句。使用这种方法,您可以重构代码的相关部分:

resp_2_data=resp_2.json()
Product.objects.get_或_create(title=item['name'],
目的地=项目['city']['name'],
description=resp_2_data['contentListHtml'][0]['html'][0:2000],
....
)

see我想你只是想把contentListHtml
description=resp_2_数据['contentListHtml'][0]['html'][:2000]
see我想你只是想把contentListHtml
description=resp_2_数据['contentListHtml'][0]['html'][:2000]
see我想你只是想把contentListHtml
description=resp_2_2_数据['contentListHtml'][0][0]!有没有办法在每个描述的末尾加上这个“…”呢?否则如果用户正在阅读&短语刚刚被删掉了,那就有点奇怪了。你可以用你想放在末尾的任何字符串连接起来,比如+运算符。酷,我刚刚做了:
description=resp_2_data['contentListHtml'][0]['html'][0:2000]+'…'
我认为它会起作用。太棒了,它能起作用!有没有办法在每个描述的末尾加上这个“…”呢?否则如果用户正在阅读&短语刚刚被删掉了,那就有点奇怪了。你可以用你想放在末尾的任何字符串连接起来,比如+运算符。酷,我刚刚做了:
description=resp_2_data['contentListHtml'][0]['html'][0:2000]+'…'
我认为它会起作用。