[英]Executing custom management commands return None
我正在嘗試創建自定義管理命令,該命令將從 Api 執行數據。 我寫了這段代碼:
from django.core.management.base import BaseCommand, CommandError
from data.models import Country
import requests
import json
def extracting():
country_req = requests.get("https://api-football-v1.p.rapidapi.com/countries", headers = {"X-RapidAPI-Key": "my_token"})
parsed_string = json.loads(country_req.text)
class Command(BaseCommand):
def handle(self, **options):
print(extracting())
但是,當我嘗試在控制台中使用python manage.py extract
執行它時,我看到 None,而當我嘗試在沒有自定義管理命令的情況下在控制台中運行此代碼時,我看到了我嘗試執行的數據。
有任何想法嗎?
您不會從extracting()
方法中返回任何內容。 根據您的交互式控制台,您可能會看到變量的值。 但你可能想要做的是:
def extracting():
country_req = requests.get("https://api-football-v1.p.rapidapi.com/countries", headers = {"X-RapidAPI-Key": "my_token"})
return json.loads(country_req.text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.