Click here to Skip to main content
15,867,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So this code is working perfectly.

However, I need the user to be able to change the value of the variable "city."

At the moment it's equal to 'London', and if I type a valid city name, the appropriate result appears on screen. I am using an API, so they have all the cities on their database.

So when they type in a city in the input field of the page, and they hit the Submit/Search button, they will change the value of the variable city and thus be shown the (weather of the) city that they are searching for.

I've been advised that this can all be done with a get form alone, but I'm not sure how to do it.

Could you please advise me?


Python
def user_homepage(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=38e4fec38e509c018629074ac1754906'
    city = 'London'
    
    r = requests.get(url.format(city)).json()
    
    city_weather = {
        'city': city,
        'temperature' : r['main']['temp'],
        'description' : r['weather'][0]['description'],
        'icon' :  r['weather'][0]['icon'],
    }
    
    result = {'city_weather' : city_weather}
    return render(request, 'userhomepage.html', result)


What I have tried:

I hope I am not breaking the rules, but I haven't actually done anything, as I am not sure how to do it.

Forms is something that I struggle with.
Posted
Updated 3-Nov-19 8:40am
Comments
Prakash Yogi 21-May-20 2:37am    
Simply use a form with post method

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900