简体   繁体   中英

How to call a view from another in Flask

So the SignUp 1 function connects to a html file, this part is all fine and the user can submit the form. However, once this form is validated, i'd like to call the SignUp2() function, which should connect another html file with a form. However, once the user submits the form (in the SignUp1 function), I get an error called: werkzeug.routing.BuildError: Could not build url for endpoint 'SignUp2'.

Please could you tell me how I can call the SignUp2 function from the SignUp 1 function. Thankyou.

@app.route('/', methods=["GET", "POST"])
def SignUp1():
    form = UserForm()
    SchoolCode = None
    FirstName = None
    Postcode = None
    Phone = None
    ConfirmEmail = None
    Password = None
    AddLineOne = None
    AddLineTwo = None
    if form.validate_on_submit():
        first_name = form.FirstName.data
        last_name = form.LastName.data
        postcode = form.Postcode.data
        phone = form.Phone.data
        email = form.Email.data
        password = form.Password.data
        add_one = form.AddLineOne.data
        add_two = form.AddLineTwo.data
        user_id = AddUser(first_name, last_name, postcode,     phone, email, password, add_one, add_two)
        Parent_id = user_id
        SignUp2(Parent_id)
    return render_template('SignUp.html', form=form)


def SignUp2(Parent_id):
    form = Child1Form()
    c1_FirstName = None
    c1_LastName = None
    c1_Year = None
    return render_template('ChildSignUp.html', form=form)

You need to give SignUp2 a route as well, and then you should redirect, rather than trying to call it directly.

(Note, all those assignments to None are pointless, you don't use any of those variables anywhere.)

@app.route('/', methods=["GET", "POST"])
def SignUp1():
    form = UserForm()
    ...
    if form.validate_on_submit():
       ...
        return redirect(url_for('SignUp2', Parent_id=Parent_id))
    ...

@app.route('/signup2', methods=["GET", "POST"])
def SignUp2(Parent_id):
    ...

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