当前位置: 首页 > 知识库问答 >
问题:

收到一个错误,指出我的字典对象在 Django 应用程序中没有属性 META

韩华美
2023-03-14

我正在尝试提取json数据并使用Django将其显示在页面上。我收到以下错误:

'dict'对象没有属性'META'

在我的 views.py 文件的第36行,如下所示:

返回渲染(请求,base_generic.html,上下文=上下文)

这是在一次搜索中发现的,但我有正确的括号来表示我的上下文,我也发现了这一点,但老实说,这只是让我更加困惑。

对于为什么会发生这种情况或者我做错了什么,你有什么建议吗?非常感谢,如果还有什么我可以提供的,让我的问题更清楚,我会这样做。感谢大家抽出时间。

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 %}

全回溯

环境:

请求方法:GET请求URL:http://127.0.0.1:8000/wx/

Django版本:2.2.3 Python版本:3.6.8已安装的应用程序:['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles']已安装的中间件:['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']

追踪:

文件“/家/道格/.virtualenvs/django/lib/python3.6/site-packages/django/core/处理程序/异常.py”在内部34中。响应 = get_response(请求)

文件"/home/doug/。virtualenvs/django/lib/python 3.6/site-packages/django/core/handlers/base . py " in _ get _ response 115。response = self . process _ exception _ by _ middleware(e,请求)

文件"/home/doug/。virtualenvs/django/lib/python 3.6/site-packages/django/core/handlers/base . py " in _ get _ response 113。response = wrapped _ callback(request,*callback_args,**callback_kwargs)

索引 36 中的文件“/家/道格/文档/开放花园/wx/views.py”。返回渲染(请求,“base_generic.html”,上下文=上下文)

render 36.content=loader中的文件“/home/doug/.virtualenvs/django/lib/python3.6/site-packages/django/structures.py”。render_to_string(模板名称,上下文,请求,using=using)

文件/home/doug/. Virtualenvs/django/lib/python3.6/site-pack/django/模板/loader.py在render_to_string62.返回template.render(上下文,请求)

文件“/家/道格/.virtualenvs/django/lib/python3.6/site-packages/django/模板/后端/django.py”在渲染61中。返回自模板渲染(上下文)

render 169.with context.bind_template(self)中的文件“/home/doug/.virtualenvs/django/lib/python3.6/site packages/django/template/base.py”:

在输入 81 中输入文件“/usr/lib/python3.6/上下文.py”。返回下一个(自我生成)

bind_template246.updates.update(处理器(self.request))中的文件"/home/doug/. Virtual alenvs/django/lib/python3.6/site-packs/django/模板/context.py"

文件"/home/doug/。debug 40中的virtualenvs/django/lib/python 3.6/site-packages/django/template/context _ processors . py。if设置。调试和请求。设置中的META.get('REMOTE_ADDR ')。内部_IPS:

异常类型: /wx的属性错误/异常值:“判决”对象没有属性“META”

共有1个答案

冯风史
2023-03-14

我的问题是我使用的变量名称“request”与函数参数冲突。一旦我改变了这一点,它就起作用了。

谢谢塞尔库克和加文的帮助。

 类似资料: