简体   繁体   中英

Can I call an function from another function in Django views.py?

I have created two functions in my views.

def Covid_view() - this one will fetch all covid data from an api.

Another function is def home() . Inside this function I want to call covid_view() which will return me data that I can pass in my template.

Below is my code:

Views.py

from django.shortcuts import render
import requests

# this function is just to call api and get data of covid
def covid_data():
    api_response = requests.get('https://covid19.mathdro.id/api/countries/india')
    covid_data = (api_response.json())
    trim_last_update = covid_data['lastUpdate']
    trimmed_date = trim_last_update.find('T') # Here I'm trimming date_time string to show only date. T was for time to I split till T and store prior string
    res_trimmed_date = trim_last_update[:trimmed_date] # This will print string till T but not T and afterwards
    confirmed = covid_data['confirmed']
    recovered = covid_data['recovered']
    deaths = covid_data['deaths']
    context = {
        'res_trimmed_date': res_trimmed_date,
        'confirmed': confirmed['value'],
        'recovered': recovered['value'],
        'deaths': deaths['value']
    }
    return context


def index(request):
    # here I want to call this function which must return me data so I can pass it to my template
    data = covid_data()
    return render(request, 'covidStatsApp/index.html', data)

When I tried to run it gives me error like even though I tried to pass request as well.

TypeError at /
covid_data() missing 1 required positional argument: 'request'

All views take an HttpRequest object as their first parameter. It should be def covid_data(request) . That should fix it. You can get more info about it in the official documentation here .

Edit: From what I know, you should always return an HttpResponse. You might have to format the returned data like return HttpResponse(context) . I haven't tried it and I'm talking from what I remember, but I think you get the idea. The documentation I cited before should have more info on this.

def covid_data(request):                                                          
      .......                                                                                           
      ...                                                                             
      return context                                                               


def index(request):                                                               
    # here I want to call this function which must return me data so I can pass it to my template                                                       
    data = covid_data(HttpRequest)                                                 
    return render(request, 'covidStatsApp/index.html', data)   

     

#That is All...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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