1
votes

I am trying to pull json data and display it on a page using Django. I am receiving the following error:

'dict' object has no attribute 'META'

on line 36 of my views.py file which is the following:

return render(request, 'base_generic.html', context=context)

This came up in a search but I have the correct brackets for my context and I also found this but it is honestly just confusing me even further.

Do you have any suggestions as to why this is happening or what I am doing wrong? Thank you so much and if there is anything else I can provide to make my question more clear I will do so. Thank you all for your time.

views.py

from django.shortcuts import render
from datetime import datetime

import requests


def index(request):

    request = requests.get(MY_URL_IS_HERE).json()

    current_temperature = request['currently']['temperature']
    current_cloud_cover = request['currently']['cloudCover']
    current_humidity = request['currently']['humidity']
    current_near_storm_distance = request['currently']['nearestStormDistance']
    current_near_storm_bearing = request['currently']['nearestStormBearing']
    current_precip_intensity = request['currently']['precipIntensity']
    current_uv_index = request['currently']['uvIndex']
    current_sunrise = datetime.fromtimestamp(request['daily']['data'][0]['sunriseTime'])
    current_sunset = datetime.fromtimestamp(request['daily']['data'][0]['sunsetTime'])

    test = 'test'

    context = {
        'current_temperature':current_temperature,
        'current_cloud_cover':current_cloud_cover,
        'current_humidity':current_humidity,
        'current_near_storm_distance':current_near_storm_distance,
        'current_near_storm_bearing':current_near_storm_bearing,
        'current_precip_intensity':current_precip_intensity,
        'current_uv_index':current_uv_index,
        'current_sunrise':current_sunrise,
        'current_sunset':current_sunset,
    }

    return render(request, 'base_generic.html', context=context)

generic_base.html

<!DOCTYPE html>
<html lang="en">
<head>
  {% block title %}<title>weather</title>{% endblock %}
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <!-- Add additional CSS in static file -->
  {% load static %}
  <link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-2">
      {% block sidebar %}
     {% endblock %}
      </div>
      <div class="col-sm-10 ">{% block content %}{% endblock %}</div>
    </div>
  </div>
</body>
</html>

index.html

{% extends "base_generic.html" %}

{% block content %}
  <ul>
    <li><strong>current_cloud_cover:</strong> {{ current_cloud_cove }}</li>
    <li><strong>current_temperature:</strong> {{ current_temperature }}</li>
    <li><strong>current_humidity:</strong> {{ current_humidity }}</li>
    <li><strong>current_near_storm_distance:</strong> {{ current_near_storm_distance }}</li>
    <li><strong>current_near_storm_distance:</strong> {{ current_near_storm_distance }}</li>
    <li><strong>current_near_storm_bearing:</strong> {{ current_near_storm_bearing }}</li>
    <li><strong>current_precip_intensity:</strong> {{ current_precip_intensity }}</li>
    <li><strong>current_uv_index:</strong> {{ current_uv_index }}</li>
    <li><strong>current_sunrise:</strong> {{ current_sunrise }}</li>
    <li><strong>current_sunset:</strong> {{ current_sunset }}</li>
  </ul>
{% endblock %}

Full traceback

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/wx/

Django Version: 2.2.3 Python Version: 3.6.8 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles'] Installed 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']

Traceback:

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 34. response = get_response(request)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 115. response = self.process_exception_by_middleware(e, request)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 113. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/doug/Documents/opengarden/wx/views.py" in index 36. return render(request, 'base_generic.html', context=context)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/shortcuts.py" in render 36. content = loader.render_to_string(template_name, context, request, using=using)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/template/loader.py" in render_to_string 62. return template.render(context, request)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/template/backends/django.py" in render 61. return self.template.render(context)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/template/base.py" in render 169. with context.bind_template(self):

File "/usr/lib/python3.6/contextlib.py" in enter 81. return next(self.gen)

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/template/context.py" in bind_template 246. updates.update(processor(self.request))

File "/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/template/context_processors.py" in debug 40. if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:

Exception Type: AttributeError at /wx/ Exception Value: 'dict' object has no attribute 'META'

1
Use a different variable name here as it conflicts with the function argument: request = requests.get(MY_URL_IS_HERE).json()Selcuk
@Selcuk wow. I have been searching for about two hours for an answer and that got rid of my problem. Now for some reason it's just showing a blank page and not my data but that's a new issue. Thank you so much for your time!DMellon
You probably want to render "index.html" instead of "base_generic.html"GavinH
@GavinH You all are amazing. Crazy how I get so caught up and overlook such minor things. Thanks so much for your time.DMellon

1 Answers

1
votes

My issue was that I was using the variable name "request" which was clashing with the function argument. It worked as soon as I changed that.

Thank you Selcuk and GavinH for your help.