django响应方法后don';t呈现到html模板
我在视图中有两个请求函数,一个是.get方法,另一个是.post。这两个函数都能正常工作,因为终端中的代码是200 [01/Apr/2021 08:04:39]“获取/搜索/搜索HTTP/1.1”200 4164 [01/Apr/2021 08:04:57]“发布/搜索/搜索HTTP/1.1”200 4164 当我尝试使用.post方法将函数呈现到html模板时,问题就出现了。html页面上没有显示任何内容django响应方法后don';t呈现到html模板,html,django,post,request,Html,Django,Post,Request,我在视图中有两个请求函数,一个是.get方法,另一个是.post。这两个函数都能正常工作,因为终端中的代码是200 [01/Apr/2021 08:04:39]“获取/搜索/搜索HTTP/1.1”200 4164 [01/Apr/2021 08:04:57]“发布/搜索/搜索HTTP/1.1”200 4164 当我尝试使用.post方法将函数呈现到html模板时,问题就出现了。html页面上没有显示任何内容 def wind_search(request): if request.method
def wind_search(request):
if request.method == 'post':
city = request.post['city']
weather_city_url = urllib.request.urlopen('api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=1a7c2a40a0734d1dc18141fc6b6241bb').read()
list_of_data = json.loads(waether_city_url)
# main wind information
wind_speed = list_of_data['wind']['speed']
# wind_gust = wea['current']['wind_gust']
wind_deg = list_of_data['wind']['deg']
# wind conversiont m/s to knots
def wind_converter(w):
knots = 2
kt = (float(w)) * knots
return kt
wind_response = wind_converter(wind_speed)
#convert wind degree in cardinal direction.
def degrees_to_cardinal(d):
dirs = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
ix = round(d / (360. / len(dirs)))
return dirs[ix % len(dirs)]
direction = degrees_to_cardinal(wind_deg)
wind_data = {
"wind_response":wind_response,
"wind_direction":direction,
}
else:
wind_data={}
context = {"wind_data":wind_data}
return render(request, 'API/wind_search.html',context)
这是html模板:
{% extends "API/base.html" %}
{% block content %}
<!--Jumbotron -->
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Wind search</h1>
<p class="lead">Write the city and check th wind condition. </p>
<!-- form input search tool -->
<nav class="navbar navbar-expand-lg navbar-dark">
<form method="post" class="col-md"">
{% csrf_token %}
<div class=" input-group">
<input type="text" class="form-control" name="city" placeholder="Choose Your City ...">
<div class="input-group-btn">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</form>
</nav>
<div class="row">
{% if wind_response and wind_direction %}
<h4><span>Wind Speed :</span> {{wind_data.wind_speed}}</h4>
<h4><span>Wind_Direction :</span> {{wind_data.wind_direction}}</h4>
</div>
{% endif %}
</div>
{% endblock content %}
{%extends“API/base.html”%}
{%block content%}
风向搜索
写下城市并检查风力状况
请设置渲染和重定向模板
def wind_search(request):
if request.method == 'POST':
#this block of code manages if there is a POST request...
city = request.POST.get('city')
weather_city_url = urllib.request.urlopen('api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=1a7c2a40a0734d1dc18141fc6b6241bb').read()
list_of_data = json.loads(waether_city_url)
# main wind information
wind_speed = list_of_data['wind']['speed']
# wind_gust = wea['current']['wind_gust']
wind_deg = list_of_data['wind']['deg']
# wind conversiont m/s to knots
def wind_converter(w):
knots = 2
kt = (float(w)) * knots
return kt
wind_response = wind_converter(wind_speed)
#convert wind degree in cardinal direction.
def degrees_to_cardinal(d):
dirs = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
ix = round(d / (360. / len(dirs)))
return dirs[ix % len(dirs)]
direction = degrees_to_cardinal(wind_deg)
wind_data = {
"wind_response":wind_response,
"wind_direction":direction,
}
# the page that you want to load after submitting your POST request <-----------------------------------------------------
return redirect( 'redirect to a view ' )
#the below block of code will cater for the GET method request
else:
wind_data={
'foo' : 'foo'
}
#the page you want to render on a Get Request <-----------------------------------------------------
return render(request,'render the required html for GET request' , wind_data)
def wind_搜索(请求):
如果request.method==“POST”:
#这段代码管理是否有POST请求。。。
city=request.POST.get('city')
weather_city_url=urllib.request.urlopen('api.openweathermap.org/data/2.5/weather?q='+city+'&appid=1a7c2a40a0734d1dc18141fc6b6241bb')。read()
数据列表=json.loads(城市url)
#主要风力资料
风速=风数据列表['wind']['speed']
#风阵风=wea['current']['wind\u gust']
风度=风数据列表['wind']['deg']
#风速转换为节数
def风电转换器(w):
节数=2
kt=(浮子(w))*节
返回kt
风响应=风转换器(风速)
#将风向转换为基本方向。
def度数到基数(d):
dirs=['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']
ix=圆形(d/(360./len(dirs)))
返回目录[ix%len(目录)]
方向=度到基数(风度)
风电数据={
“风响应”:风响应,
“风向”:风向,
}
#提交POST请求后要加载的页面尚未呈现POST响应。您的程序中只有一个渲染,请您更具体一点,我不明白。我的意思是函数末尾的渲染对POST方法不起作用?行context={“wind\u data”:wind\u data}
不应该缩进。现在,它是else:
块的一部分。还要检查模板中的风响应
和风向
参考,以确保它们与上下文
数据匹配。我移除缩进并检查参考,但仍然相同,还有其他建议吗?