简体   繁体   中英

Getting hidden input value from form through Flask/Python

I wanted to get the value from hidden input (or any alternative methods) in order to save it in a file called wishlist.json. However, I'm only getting Null/None when I tried. Can anyone help?

Flask py

def wishlist():
    wishlistObject = ""
    email = currentUser
    try:
        name = request.args.get('productName')
        url = request.args.get('productURL')
        print(name)
        wishlistObject = addNew(email, url, name)
        return wishlistObject
    except Exception as e:
        print(e)
        pass
    return wishlistObject

Html

{% block body %}
<p><a href="http://127.0.0.1:5000/"><img src="https://i.ibb.co/VBwQqyH/uub8y3.png" alt="logo" width=100 length=100></a> </p>
<section class="modal-content animate">
   <div class="container">
        <h2 class="title-headline" style="text-align:center">List product(s) found!</h2>
        {% for item in items %}
        <div class="card">
            <a href="{{item['url']}}">
                <img src="{{item['image']}}" alt="{{item['name']}}" style="width: 100%">
            </a>
            <h2>{{item['name']}}</h2>
            <p>Price: {{item['price']}}</p>
            <p>Store: {{item['store']}}</p>
            <form action="/wishlist" method="post">
                <input type="hidden" id="productName" name="productName" value="{{item['name']}}">
                <input type="hidden" id="productURL" name="productURL" value="{{item['url']}}">
                <button type="submit" onClick="{{ url_for('wishlist') }}">Wishlist</button>
            </form>
        </div>
        {% endfor %}
    </div>
</section>
{% endblock %}

value display on html在此处输入图片说明

I think I solved by doing this:

def wishlist():
    wishlistObject = ""
    email = currentUser
    try:
        name = request.form['productName']
        url = request.form['productURL']
        wishlistObject = addNew(email, url, name)
        return wishlistObject
    except Exception as e:
        print(e)
        pass
    return wishlistObject```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM