简体   繁体   English

如何使用 python Django 和 django-rest-framework 解构 API?

[英]How do I destructure an API with python Django and django-rest-framework?

I have a successfully compiled and run a django rest consuming cocktaildb api.我已经成功编译并运行了一个 django rest 消耗的cocktaildb api。 On local server when I run http://127.0.0.1:8000/api/ I get在本地服务器上运行http://127.0.0.1:8000/api/我得到

{
    "ingredients": "http://127.0.0.1:8000/api/ingredients/",
    "drinks": "http://127.0.0.1:8000/api/drinks/",
    "feeling-lucky": "http://127.0.0.1:8000/api/feeling-lucky/"
}

But when I go to one of the links mentioned in the json result above, for example:但是当我转到上面 json 结果中提到的链接之一时,例如:

http://127.0.0.1:8000/api/ingredients/

I get an empty [] with a status 200OK!我得到一个空的[] ,状态为 200OK!

I need an endpoint to GET drinks and ingredients before I can destructure to specific details using angular.我需要一个端点来获取饮料配料,然后才能使用 angular 分解为特定细节。

I implemented helper folder in the app with the the API function as below:我在应用程序中使用 API 函数实现了 helper 文件夹,如下所示:

class TheCoctailDBAPI:

    THECOCTAILDB_URL = 'https://www.thecocktaildb.com/api/json/v1/1/'

    async def __load_coctails_for_drink(self, drink, session):
        for i in range(1, 16):
            ingredientKey = 'strIngredient' + str(i)
            ingredientName = drink[ingredientKey]

            if not ingredientName:
                break

            if ingredientName not in self.ingredients:
                async with session.get(f'{TheCoctailDBAPI.THECOCTAILDB_URL}search.php?i={ingredientName}') \
                        as response:
                    result = json.loads(await response.text())
                    self.ingredients[ingredientName] = result['ingredients'][0]

What was your expected responce?你预期的反应是什么?

Add the function that is called by this API as well as the DB settings in the question, so that we can properly help you.添加此API调用的函数以及问题中的DB设置,以便我们可以为您提供适当的帮助。

Are you sure that you are connecting and pulling data from a remote location?您确定要从远程位置连接和提取数据吗? It looks to me like your local DB is empty, so the API has no data to return.在我看来,您的本地数据库是空的,因此 API 没有要返回的数据。

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

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