0

I am trying to get dirname value on clicking the link of "More Photos". Here is the Form HTML.

<form action="/single" method="GET">
   <input type="text" name="dirname" value="{{ dir_name }}" style="display:none">
   <a href="single.html" class="btn btn-outline-white py-2 px-4">More Photos</a>
</form>

Here is the function in flask to get this data.

@app.route('/single.html', methods=['GET', 'POST'])
@app.route('/single', methods=['GET', 'POST'])
def single():
    print(request.method)
    if request.method == "GET":
        dir_name = request.args.get("dirname")
        print("Dir name is ", dir_name)
        main_path = join("static/Downloaded_images", dir_name)
        images_paths, names = get_paths(main_path)
    return render_template('single.html', images_paths_names=zip(images_paths,names))

this is the URL when I click More photos. http://127.0.0.1:5000/single.html

How do I pass dirname in URL to get it in function? Currently it is showing me None.

shahid hamdam
  • 751
  • 1
  • 10
  • 24
  • In your jinja template, you are expecting var name of `dir_name` here: `value="{{ dir_name }}"`, but in your Flask code, you only pass in `images_paths_names` parameter to `single.html` template page. That's why your `dir_name` is always `None`. You might try this `return render_template('single.html', images_paths_names=zip(images_paths,names), dir_name=dir_name)` – nngeek Jul 06 '21 at 13:23
  • @nngeek please check question now – shahid hamdam Jul 06 '21 at 13:26
  • On clicking anchor tag, the method is always GET no matter what I mention in form. – shahid hamdam Jul 06 '21 at 13:27

0 Answers0