werkzeug.exceptions.BadRequestKeyError:400错误的请求:浏览器(或代理)发送了该服务器无法理解的请求

时间:2020-02-17 15:48:01

标签: flask

在我的代码中,有2种形式:1在登录页面上,其他在myapp页面上。当我在登录页面上按“提交”按钮时,它将直接显示keyerror'emp-name'..我将无法看到myapp.html页面。 提交用户名和密码后,我想在myapp页面上发布员工代码。但是我的代码并没有发生这种情况。

app.py:

from flask import Flask, flash, redirect, render_template, request, session, abort,send_file
import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from tabledf import *

engine = create_engine('sqlite:///tutorial.db', echo=True)
app = Flask(__name__)
@app.route('/')
def home():
    return  "Hello Employee <a href='/login'>Login</a>"

@app.route('/login')
def login():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        return render_template('main.html')

@app.route('/login', methods=['POST'])
def do_admin_login():

   POST_USERNAME = str(request.form['username'])
   POST_PASSWORD = str(request.form['password'])

   Session = sessionmaker(bind=engine)
   s = Session()
   query = s.query(User).filter(User.username.in_([POST_USERNAME]), User.password.in_([POST_PASSWORD]) )
   result = query.first()
   if result:
       session['logged_in'] = True
       return render_template('main.html')
   else:
       flash('wrong password!')
       return render_template('login.html')
#   return home()

@app.route('/myapp', methods=['POST'])
def parse(): 
    stock = request.form['stock-name']
    function(stock)
    return render_template('download.html')

登录html:

<html>
  <head>
    <title>Risk - login page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="static/bootstrap.min.css" rel="stylesheet" media="screen">

     <link rel="stylesheet" href="/static/style.css" type="text/css">
  </head>
  <body>
    <form action='/myapp' method="POST">
    <div class="login">
        <div class="login-screen">
            <div class="app-title">
                <h1>Login</h1>
            </div>
        <div class="login-form">
    <div class="control-group">
                <input type="text" class="login-field" value="" placeholder="Username" name="username">
<label class="login-field-icon fui-user" for="login-name"></label></div>
<div class="control-group">
                <input type="password" class="login-field" value="" placeholder="Password" name="password">
<label class="login-field-icon fui-lock" for="login-pass"></label></div>
<input type="submit" value="Log in" class="btn btn-primary btn-large btn-block">

</div>
</div>
</div>
</form>
  </body>
</html>

myapp.html:

<!DOCTYPE html>
<head>
   <title>My title</title>
</head>

<body style="text-align:center;>  
<div class="header">
    img---
    </div>
    <div class="container">

    <div class="sign-up" id="sign-up">
                <form action=''>
                    <div class="form-group">
            <h3 style="color:black;font-size:150%;text-align:center;vertical-align:middle;">REQUIRED emp </h3
                        <label for="user-name"></label>
                        <input type="text" class="form-control" name="emp-name" id="user_emp-name" placeholder="Enter emp code " size=25>
                    </div>
                  <input type="submit" value="Submit" >

                </form>
            </div>

 </div>

0 个答案:

没有答案