'dajango AttributeError at / 'WSGIRequest' object has no attribute 'get'

This is my views.py

from django.shortcuts import render
    import requests
    
    # Create your views here.
    def mainfun(requests):
        city="London"
        url=f"http://api.weatherapi.com/v1/current.json?key=69528a98f894438b88982548221507&q={city}&aqi=no"
        data= requests.get(url)
        return render(requests,"index.html",{'d':data})

This is my settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'weatherapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'weather.urls'

requests is not working when try to get data from the api.. How to solve this problem..?



Solution 1:[1]

Your parameter is also named requests and thus overrides the reference to the requests library. Normally the first parameter of a view is named request, so:

from django.shortcuts import render
import requests

# Create your views here.
def mainfun(request):
    city="London"
    url=f"http://api.weatherapi.com/v1/current.json?key=69528a98f894438b88982548221507&q={city}&aqi=no"
    data= requests.get(url)
    return render(requests, 'index.html', {'d':data})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem