2016-10-11 2 views
0

for 루프에 Flask-WTF 양식 필드가 있습니다. 각 품목에 대해 새 수량을 전기하고 싶습니다.루프에있는 플라스크 wtforms 게시 및 데이터 저장

필드 목록에 대해 읽었습니다. 나는 아직도 이해하지 못한다. 그러나 나는 그들이 대답일지도 모른다라고 생각한다.

@app.route('/checkout') 
def checkout(): 
    form = CartForm() 
    for item in current_user.cart: 
     product = Product.query.get(item.product_id) 
     cart_items.append({product: item.quantity}) 

    return render_template('checkout.html',cart_items=cart_items,form=form) 



{% for item in cart_items %} 
    {% for product, quantity in item.items() %} 
     {{product.name}} 
     {{product.price}} 
     {{form.quantity }} 
    {% endfor %} 
{% endfor %} 

Problem1 : 각 플라스크-WTF 폼 필드 각각에 걸쳐 반복 할 때 같은 이름을 가지고있다.

출력

<select id="quantity" name="quantity"><option value="1">1</option></select> 
<select id="quantity" name="quantity"><option value="1">1</option></select> 

problem2 : 각 양식은 이름이 다른 경우 백업에 저장하는 방법.

+0

내 솔루션을보십시오 :) –

답변

1

동일한 문제가 발생했습니다. 하지만 Flask-WTF없이이 문제를 해결했습니다. 아래 솔루션은 내 앱을 기반으로했습니다. 앨범 편집 페이지가 있는데 앨범의 각 그림에 텍스트 입력을 반복 한 다음 각 그림의 텍스트를 저장해야합니다.

<form action="{{ url_for('edit_photo', id=album.id) }}" method="POST"> 
<ul> 
    {% for photo in photos %} 
    <li> 
     <img class="img-responsive portrait" src="{{ photo.path }}" alt="Some description"/> 
     <textarea name="{{ photo.id }}" placeholder="add some description" rows="3">{% if photo.description %}{{ photo.description }}{% endif %}</textarea> 
    </li> 
    {% endfor %} 
</ul> 
<hr> 
<input class="btn btn-success" type="submit" name="submit" value="submit"> 

그럼 루프 사진 :

I 루프 HTML에서 입력, 내가 다른보기 기능과 사용하는 각 텍스트 입력의 이름 각 사진의 ID에 대한 액션 값을 설정 통지 및 입력 데이터를 저장 (얻을 그것으로 값이 ID의의) :

@app.route('/edit-photo/<int:id>', methods=['GET', 'POST']) 
@login_required 
def edit_photo(id): 
    album = Album.query.get_or_404(id) 
    photos = album.photos.order_by(Photo.order.asc()) 
    if request.method == 'POST': 
     for photo in photos: 
      photo.about = request.form[str(photo.id)] 
      db.session.add(photo) 
     return redirect(url_for('.album', id=id)) 
    return render_template('edit_photo.html', album=album, photos=photos) 
+0

그것들은 고유 한 이름을주고 싶기 때문에 내가 원했던 것이 아니기 때문에 이것으로 javascript를 추가 할 수 있습니다. 매우 유용했습니다. –

+0

이름 값에 대해'photo.id'를 사용합니다 : {{photo.id }} "", 그래서 모든 입력은 고유 한 이름을 갖습니다. –

+0

네, 고맙습니다. :) –

관련 문제