jquerydjangodjango-templatesyahoo-weather-api

How to load weather icons with each day of yahoo weather forecast?


I'm new to Django and jquery. I was trying to make a Weather app using Yahoo Weather API. Everything went fine except the weather icons in the forecast .Previously the same icon was used for all changing weather but how can i write code so that different icons can be seen for varying climates

Here is my Jquery code :

$(document).ready(function () {
    var content = $('#patti').text();
    var iconurl = 'http://l.yimg.com/a/i/us/we/52/' +content+ '.gif';
    $('#wicon').attr('src', iconurl);


    $.each($('.icon1'), function( ) {
        var content2 = $(this).text();
        //console.log("content=",content2);
        var iconurl2 = 'http://l.yimg.com/a/i/us/we/52/' +content2+ '.gif';
        console.log(iconurl2);
        var final = $('.wicon2').attr('src', iconurl2);
        console.log(final)
    });
});

My index.html file for the forecast of 7 days :

<div id="container-fluid">
<div class="row">
    {% if form.is_valid %}
    <div class="col-sm-3">
        <div class="container text-center hi" style="background: whitesmoke"><br>
            <iframe class="clock" src="http://free.timeanddate.com/clock/i66pyson/n1906/fn4/tct/pct/ftb/ta1" frameborder="0" width="101" height="18" allowTransparency="true"></iframe>

            <h4 style="color: whitesmoke">{{text}}</h4>
            <br>
            <div id="icon"><img id="wicon" src="" alt="Weather icon"></div><h2 id="thendi" style="color: whitesmoke"><strong>{{temp}} °C</strong></h2>
            <br>
            <!--<p style="color: whitesmoke">{{date|slice:':3'}}</p>-->
            <p id="patti" style="visibility: hidden">{{code}}</p>
        </div>
    </div>
    {% endif %}
        <br>
    <br>
    <!--forecast-->
    <div class="col-md-offset-9">
        <div class="row">
            {% for obj in forecast|slice:":7" %}
                <div class="col-sm-offset-5 text-center" style="color: #7a43b6">
                    <div class="card">
                        <p style="color: whitesmoke"> {{obj.date}}</p>
                        <p style="color: whitesmoke">{{obj.text}}</p>
                        <div class="card-content">
                            <p class='icon1' style="visibility: hidden">{{obj.code}}</p>
                            <div id="icon2"><img class="wicon2" src="" alt="Weather icon"></div>
                            <p style="color: whitesmoke"><i class="fas fa-arrow-up"></i>{{obj.high}}°C</p>
                            <p style="color: whitesmoke"><i class="fas fa-arrow-down"></i>{{obj.low}}°C</p>
                        </div>
                    </div>

                </div>
            {% endfor %}
        </div>
    </div>
</div>

This is the result I'm getting now.

enter image description here

Here is the view part :

def weatherview(request):
form = LocationForm(request.POST or None)
if form.is_valid():
    forminput = form.cleaned_data['location']
    baseurl = "https://query.yahooapis.com/v1/public/yql?"
    yql1 = 'select * from '
    yql2 = 'weather.forecast where woeid in '
    yql3 = '(select woeid from geo.places(1) where text="' + forminput+ '") and u="c"'
    yql_query = yql1+yql2+yql3
    yql_url = baseurl + urllib.parse.urlencode({'q': yql_query}) + "&format=json"
    result = urllib.request.urlopen(yql_url).read()
    data = json.loads(result)
    forecast  =   data['query']['results']['channel']['item']['forecast']
    location = data['query']['results']['channel']['location']
    today = data['query']['results']['channel']['item']['condition']


    return render(request,'index.html', {
        "forecast":forecast,
        "city":location['city'],
        "country": location['country'],
        "region": location['region'],
        "date":today['date'],
        "temp":today['temp'],
        "text":today['text'],
        'code':today['code'],
        'form': form
    })

return render(request,'index.html',{'form':form})

Solution

  • The code is all we need to display the image. You can use the {{ code }} in src to display picture.

    <img src="http://l.yimg.com/a/i/us/we/52/{{ code }}.gif">