-1

I have 2 pages , they are search and results, i want to get data from search and display in Results i'm trying to pass through URL but i'm not able to find the solution for it


@views.route('/search',methods=["GET","POST"])
def search():
     if request.method == "POST":
        city_name = request.form['city_name']
        return redirect(url_for("results",city_name=city_name))
     return render_template("search.html")


@views.route('/results/<city_name>')
def results(city_name):
    geolocator = Nominatim(user_agent="MyApp")
    location = geolocator.geocode("Hyderabad")
    latitude=  location.latitude
    longitude= location.longitude
    t = time.localtime()
    current_time = time.strftime("%I:%M", t)
    today = date.today()
    d4 = today.strftime("%b-%d-%Y")

    HTML_current_time = str(current_time) + " " + str(d4)
    
    return render_template("results.html", current_time=HTML_current_time,latitude=latitude,longitude=longitude,city_name=city_name)

the search page form

 <form  action="{{ url_for('views.results',city_name=city_name) }} " method="post">
                <input type="text" name="text" id="search" placeholder="Enter the place area...">
                <button type="submit" class="btn"> <img src="/static/images/search.png" alt=""></button>
           </form>

1 Answers1

0

In your form, remove the action logic and change name="text" to name="city_name". You don't need the url_for syntax, that is causing you to redirect to your results from the form action. You need to submit the form, and get the result from city_name, via your 2 line in your search route, and then redirect to your results route from there, like you're doing in line 3 of your search route.

Andrew Clark
  • 850
  • 7
  • 13