【问题标题】:django response method POST don't render to the html templatedjango 响应方法 POST 不渲染到 html 模板
【发布时间】:2021-06-28 06:06:30
【问题描述】:

我在视图中有两个请求函数,一个是 .get 方法,另一个是 .post。这两个函数都可以正常工作,因为在终端中代码是 200。

[2021 年 4 月 1 日 08:04:39] “GET /search/search HTTP/1.1”200 4164 [01/Apr/2021 08:04:57]“POST /search/search HTTP/1.1”200 4164

当我尝试使用 .post 方法将函数渲染到 html 模板时,问题就出现了,html 页面上没有出现任何内容。

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

我认为问题在于 html,因为基本上视图不显示任何错误消息,所以我尝试更改几次 html 代码但没有成功。任何帮助/解释都很好。

非常感谢。

【问题讨论】:

  • 您尚未呈现 POST 的响应。您的程序中只有一个渲染
  • 你能不能再具体点我不明白。我的意思是函数末尾的渲染也不适用于 POST 方法?
  • context = {"wind_data":wind_data} 行不应缩进。就像现在一样,它是else: 块的一部分。
  • 还要检查模板中的 wind responsewind direction 引用,确保它们与 context 数据匹配。
  • 我删除了缩进并检查了引用但仍然相同,还有其他建议吗?

标签: html django post request


【解决方案1】:

请设置渲染和重定向模板,

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)
        

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 2021-02-17
    • 2017-10-12
    • 2015-06-23
    • 2017-02-11
    • 1970-01-01
    • 2013-06-27
    • 2013-10-08
    • 2013-06-26
    相关资源
    最近更新 更多