简体   繁体   English

Django 视图 - 使用请求

[英]Django views - Using requests

If I have a views.py function like this one right here:如果我在这里有一个像这样的 views.py 函数:

#imports

env = environ.Env()
environ.Env.read_env()

 def stocks(request):
    input_ticker = request.POST['ticker']
    response = requests.get(f"https://cloud.iexapis.com/v1/stock/" + 
    input_ticker + "/quote?displayPercent=true&token=" + env('API_IEX'))

    parsed = response.json()

return render(request, 'result_api.html', {'api': parsed})

How can I scale up the flexibility and efficienty of my code if I want to add like 10-15 similar requests?如果我想添加 10-15 个类似的请求,如何提高代码的灵活性和效率?

(...)
response2 = requests.get(f"https://cloud.iexapis.com/anything/anything/" + 
    input_ticker + "/anything=true&token=" + env('API_IEX'))

response3 = requests.get(f"https://cloud.iexapis.com/anything/anything2/" + 
         input_ticker + "/anything2=true&token=" + env('API_IEX'))
(...)

parsed2 = response2.json()
parsed3 = response2.json()
(...)

return render(request, 'result_api.html', {'api': parsed,
                                           'api2': parsed2,
                                           'api3': parsed3 ,            })

It would be pretty munch repeated, so I think there need to be a better way to solve this here.这将是非常重复的,所以我认为这里需要一个更好的方法来解决这个问题。

PS: I am more into Django than Python atm. PS:我更喜欢 Django 而不是 Python atm。 Probably I miss something obvious out here :D可能我在这里错过了一些明显的东西:D

How can I scale up the flexibility and efficienty of my code if I want to add like 10-15 similar requests?如果我想添加 10-15 个类似的请求,如何提高代码的灵活性和效率?

I don't know much about eficciency but there's a little trick you can do to "automate" coding requests instead of copy-pasting them all it envolves arrays and string_replacements我对效率知之甚少,但是您可以做一些小技巧来“自动化”编码请求,而不是复制粘贴它们,它涉及数组和 string_replacements

max_requests=15 #change it to N

response_list=[]
#note that this loop goes 0-14 but you can change it if you like
for i in range(0,max_requests):
    response_list.append( requests.get(f"https://cloud.iexapis.com/anything/anything{i}/" +
         input_ticker + "/anything{i}=true&token=" + env('API_IEX')) )
    

parsed_list=[]
for i in range(0,max_requests):
    parsed_list.append( response_list[i].json() )

return_dict={}
for i in range(0, max_requests):
    return_dict[f"api{i}"]=parsed_list[i]
    #the end result will have api0,api1 instead of api,api1
    
return render(request, 'result_api.html',return_dict)

There are more way to make this code more readable and stuff, but this is what I could come up with before leaving.有更多方法可以使这段代码更具可读性和内容,但这是我在离开之前可以想到的。

In regard of efficiency, it looks like you're using DJango, there should be some libs to optimize lots of requests, BUT if you dont want to lib-hunt you can always think of paralelism of asyncronous request, however I am still a noob on this area.在效率方面,看起来你正在使用 DJango,应该有一些库来优化大量请求,但是如果你不想 lib-hunt 你总是可以考虑异步请求的并行性,但是我仍然是一个菜鸟在这个区域。

EDIT: another try:编辑:再试一次:

max_requests=15 #change it to whatever you like
return_dict=dict(zip([f"api{i}" for i in range(0,max_requests)],[request.json() for request in [requests.get(f"https://cloud.iexapis.com/anything/anything{i}/" +
     input_ticker + "/anything{i}=true&token=" + env('API_IEX')) for i in range(0,max_requests)]]))
return render(request, 'result_api.html',return_dict)

if it works it would be nice (but i'm not sure if request.get() works over list comprehension)如果它有效,那就太好了(但我不确定 request.get() 是否适用于列表理解)

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

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