首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法将视图中的变量发送到html python

无法将视图中的变量发送到html python
EN

Stack Overflow用户
提问于 2018-05-31 03:14:58
回答 2查看 42关注 0票数 0

我无法将视图中的temp_k变量发送到index.html。我已经从天气API提出了一个请求,并试图将其传递给我的html。请建议我们应该如何将变量从视图发送到模板。

views.py

from django.template.loader import render_to_string
import requests
from django.shortcuts import render
from django.http import HttpResponse

def hello(request):
   my_dict = {'insert_me': "From andu.py"}
   return render(request, 'mywebapp/index.html', context=my_dict)

def temperature(request):
   #zip=requests.form['zip']ss
   r=requests.get('http://samples.openweathermap.org/data/2.5/weather?zip=94040,us&appid=b6907d289e10d714a6e88b30761fae22')
   json_object=r.json()
   my_dict1={'temp_k' :json_object['main']['temp']}
   return render(request,'mywebapp/index.html', context=my_dict1)

index.html

<!DOCTYPE html>
{% load staticfiles%}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hey</title>
    <link rel="stylesheet" href={% static "cs/style.css" %}/>
</head>
<body>
    <h1>This is the Header</h1>
    <img src="{% static "images/youownme.jpeg" %}" atl="Uh Oh">
    <h2> Temperature:{{temp_k}}</h2>
</body>
</html>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50611955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档