如何使用窗体更改Flask会话字典中的值? [英] How to change values in Flask session dictionary using forms?

查看:136
本文介绍了如何使用窗体更改Flask会话字典中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的购物车基于会话并形成字典,如下所示:

  [{'qty':1,'product_title':'Post num six','product_id':6},
{'qty':1,'product_title ':'Post 5','product_id':5},
{'qty':1,'product_title':'Fouth post','product_id':4}]

我需要在购物车中的每个产品的 /购物车路线中更改 qty

  @ app.route('/ cart',methods = ['GET','POST'])
def shopping_cart():
page_header =Cart

if request.method == request.form中的'POST'和'update_cart':$ b $ for cart_item in session [ cart]:
cart_item [qty] + = 1#这是错误的

如果cart_item [qty]> 10:
flash(u'cartax中的最大金额(10)产品已达到','info')
cart_item [qty] = 10

flash(u' ('cart / cart.html',page_header = page_header)

以下是我的 cart.html 模板:

  .. 。
{%if session.cart%}

< form action =method =post>
< div class =table-responsive>
< table class =table table-hover>
< thead>
< tr>
< th>名称< / th>
< th width =100> Qty< / th>
< / tr>
< / thead>
< tbody>
{%for cart_item在session.cart%}
< tr>
< td>< a href =/ post / {{cart_item.product_id}}> {{cart_item.product_title}}< / a>< / td>

< td>
< div class =form-group>
< input class =form-controltype =numbername =change_qtymin =1max =10value ={{cart_item.qty}}>
< / div>
< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
< / div>

< div class =clearfix>
< button type =submitname =update_cartclass =btn btn-info pull-right>更新购物车< / button>
< / div>
< / form>

{%else%}
< h2 style =color:red;>没有购物车会话=(< / h2>
{%endif% cart.html包含周期中的所有实例,并添加 change_qty 输入改变购物车中每个产品的数量。


问题是:如何改变每个产品在购物车中的数量?

解决方案

会话对象不会自动检测对可变结构的修改,因此您需要设置
会话。修改= True 自己。


I'm writing shopping cart on Flask.

My cart based on session and forming dictionary like this:

[{'qty': 1, 'product_title': 'Post num six', 'product_id': 6}, 
{'qty': 1, 'product_title': 'Post five', 'product_id': 5}, 
{'qty': 1, 'product_title': 'Fouth post', 'product_id': 4}]

I need to change qty value in /cart route for every product in cart:

@app.route('/cart', methods=['GET', 'POST'])
def shopping_cart():
    page_header = "Cart"

    if request.method == 'POST' and 'update_cart' in request.form:                
        for cart_item in session["cart"]:
            cart_item["qty"] += 1 # this is wrong

            if cart_item["qty"] > 10:
                flash(u'Maximum amount (10) products in cart is reached', 'info')
                cart_item["qty"] = 10

        flash(u'update_cart', 'warning')

    return render_template('cart/cart.html', page_header=page_header)

Here is my cart.html template:

...
{% if session.cart %}

<form action="" method="post">
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Name</th>
                    <th width="100">Qty</th>
                </tr>
            </thead>
            <tbody>
            {% for cart_item in session.cart %}
                <tr>
                    <td><a href="/post/{{ cart_item.product_id }}">{{ cart_item.product_title }}</a></td>

                    <td>
                        <div class="form-group">
                            <input class="form-control" type="number" name="change_qty" min="1" max="10" value="{{ cart_item.qty }}">
                        </div>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="clearfix">
        <button type="submit" name="update_cart" class="btn btn-info pull-right">Update Cart</button>
    </div>
</form>

{% else %}
    <h2 style="color: red;">There is no cart session =(</h2>
{% endif %}

In cart.html covers all instances in cycle and adds change_qty input to change quantity of each product in cart.

The question is: how i can change quantity of each product in cart?

解决方案

The session object doesn't detect modifications to mutable structures automatically so you need to set session.modified = True yourself.

这篇关于如何使用窗体更改Flask会话字典中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆