2017-06-15 42 views
1

如何使POST方法适用于烧瓶在同一页上的两种不同形式?如何使POST方法适用于烧瓶在同一页上的两种不同形式?

下面是主要__int__.py文件瓶/蟒蛇我的代码

@app.route('/', methods=['GET', 'POST']) 
@app.route('/login/', methods=['GET', 'POST']) 
def login(): 
    error = None 
    if request.method == 'POST': 
     if request.form['email'] != '1' or \ 
       request.form['password'] != '1': 
      flash('Invalid Credentials') 
     else: 
      flash('You were successfully logged in') 
      return redirect(url_for('dashborad')) 
    return render_template('login.html', error=error) 


@app.route('/', methods=['GET', 'POST']) 
@app.route('/register/', methods=['GET', 'POST']) 
def register(): 
    try: 
     if request.method == 'POST': 
      email = request.form['email1'] 
      name = request.form['name1'] 
      password = request.form['password1'] 

      cur, db = connection() 
      x = cur.execute(" SELECT * FROM users WHERE email = (%s)" ,[escape(email)]) 

      if int(x) > 0: 
       flash ("email already used ") 
       return render_template ("register.html") 
      else: 
       cur.execute("INSERT INTO users (first_name, email, password) VALUES (%s, %s, %s);" ,(escape(name), escape(email), escape(password))) 

       db.commit() 
     return render_template ("register.html") 

现在蟒蛇正在读POST方法首先@ app.route登录表单只

有没有简单的方法来获得这样的在python中的行?

if request.method == 'POST' for form 1 

if request.method == 'POST' for form 2 

回答

0

我猜你想要的东西像LinkedIn

起始页为了实现在瓶创建HTML模板:

<input type="submit" name="btn" value="Save"> 
 
<input type="submit" name="btn" value="Cancel">

,然后你可以验证通过:

if request.form["btn"]=="Save": 
    if request.method== 'POST': 
     doSomething() 

这种解决方案可能会奏效

+0

感谢您的回答,但它给了这个错误 400错误请求:浏览器(或代理)发送的请求该服务器无法理解。 –

+0

[使用此](https://stackoverflow.com/questions/21949452/wtforms-two-forms-on-the-same-page) – vutsuak

+0

我想我必须使用WTForms毕竟 我有一个项目使用作为尽可能少的框架 再次感谢好友 –

相关问题