因此,我一直试图使这个工作,但同时,我不明白其中的一些代码意味着。我很抱歉提了这么长时间的问题,但我想了解一下这些问题是如何工作的。
我正在尝试向另一个API发出HTTP请求,以便使用django进行POST和GET方法。基于网站代码示例,即url:https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html
由于我希望在API上使用HTTP请求来调用其他API,因此我希望更好地了解这些API是如何工作的以及如何使用它。
代码在网站的底部。但是我会在这里提供代码,这样你就更容易了。
网站代码
from django_twilio.views import twilio_view
from twilio.twiml import Response
import requests
import json
BASE_URL = 'http://pokeapi.co'
def query_pokeapi(resource_uri):
url = '{0}{1}'.format(BASE_URL, resource_uri)
response = requests.get(url)
if response.status_code == 200:
return json.loads(response.text)
return None
@twilio_view
def incoming_message(request):
twiml = Response()
body = request.POST.get('Body', '')
body = body.lower()
pokemon_url = '/api/v1/pokemon/{0}/'.format(body)
pokemon = query_pokeapi(pokemon_url)
if pokemon:
sprite_uri = pokemon['sprites'][0]['resource_uri']
description_uri = pokemon['descriptions'][0]['resource_uri']
sprite = query_pokeapi(sprite_uri)
description = query_pokeapi(description_uri)
message = '{0}, {1}'.format(pokemon['name'], description['description'])
image = '{0}{1}'.format(BASE_URL, sprite['image'])
frm = request.POST.get('From', '')
if '+44' in frm:
twiml.message('{0} {1}'.format(message, image))
return twiml
twiml.message(message).media(image)
return twiml
twiml.message("Something went wrong! Try 'Pikachu' or 'Rotom'")
return twiml
我的问题是:
request.POST
和request.POST.get
的文章,但我仍然不明白。request.POST
= POST方法/create函数不是吗?body.lower
是什么意思?似乎找不到任何关于它的东西。pokemon['sprites']
是指api中的sprites字段吗?
request.POST.get('From', '')
不发布用户输入数据的位置吗?“从哪里来”?这意味着什么?如果在frm中发现了if '+44' in frm:
+44?
发布于 2018-01-18 10:03:51
所有问题都是基于非常基本的python概念,我建议您在这里阅读python ( Python文档 )。
https://stackoverflow.com/questions/48318139
复制相似问题