Forms not validating properly after first validation

All code is provided below!

I am building a website which has a flask_wtf form that the user fills in, upon validation it sends a GET request to an endpoint which in turn creates a stripe checkout session (POST-Redirect-GET pattern). The first time I fill in and submit the form it works perfectly and redirects to Stripe checkout, however subsequent attempts will not redirect to stripe but just to the beginning of the form page again (with everything filled in).

I have already tried different things to try to get it working:

  • Use sessions and update the csrf_token after each validated submit. session['csrf_token'] = generate_csrf()
  • Clear form data after each validated submit: form = RegistrationFormNutrition(formdata=None)

However, both of these methods did not work. Now I am lost and don’t know what to try next to resolve this problem. So I have come here to seek the answers, hopefully one of you can point me in the right direction. The code for the endpoints is provided below.

Endpoint that validates form:

schedule.route('/schedule/fitness', methods=['POST', 'GET'])
def schedule_fitness():
    form = RegistrationFormFitness()

    if form.validate_on_submit():
        schedule_type = 'fitness'
        schedule_item_id = create_general_schedule_order_entry(form.data, schedule_type)

        session['csrf_token'] = generate_csrf()    # tried this, did not work
        form = RegistrationFormNutrition(formdata=None) # tried this, did not work

        return redirect(
            url_for('schedule.initiate_checkout',
                    email=form.data["email"],
                    order_id=schedule_item_id,
                    schedule_type=schedule_type
                )
        )

    return render_template('schedule_fitness.html', form=form)

Endpoint that initiates stripe checkout:

schedule.route('/initiate_checkout', methods=['GET'])
def initiate_checkout():
    email = request.args.get('email')
    order_id = request.args.get('order_id')
    schedule_type = request.args.get('schedule_type')

    return create_checkout_session(email, order_id, schedule_type)

Function that creates stripe checkout session:

def create_checkout_session(email, order_id, schedule_type):
    stripe.api_key = stripe_keys["secret_key"]
    line_items = TEST_PRODUCT  
    domain_url = "http://localhost/"
    
    try:
        checkout_session = stripe.checkout.Session.create(
            success_url=domain_url + f"schedule/order?email={email}&order_id={order_id}           &schedule_type={schedule_type}",
            cancel_url=domain_url + "cancelled",
            payment_method_types=["card", "ideal", "paypal"],
            mode="payment",
            metadata={
                "order_id": str(order_id),
                "schedule_type": schedule_type
            },
            line_items=line_items,
        )
        return redirect(checkout_session.url, code=303)
    except Exception as e:
        print(e)
        return jsonify(error=str(e)), 403

I have already tried different things to try to get it working:

  • Use sessions and update the csrf_token after each validated submit. session[‘csrf_token’] = generate_csrf()
  • Clear form data after each validated submit: form = RegistrationFormNutrition(formdata=None)

New contributor

mh_dss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật