当前位置: 首页 > 知识库问答 >
问题:

错误的请求浏览器(或代理)发送了此服务器无法理解的请求

金慈
2023-03-14

我已经试过填充add。html,当我单击send时,我得到上面的错误。错误的请求浏览器(或代理)发送了此服务器无法理解的请求。我的应用程序。py如下所示。

from flask import Flask, render_template, url_for, redirect, request

from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import requests

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://user:mypassword@localhost/mydb' 

db=SQLAlchemy(app)
# class User(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(80), unique=True, nullable=False)
#     email = db.Column(db.String(120), unique=True, nullable=False)

#     def __init__(self , username, email):
#         self.username = username
#         self.email = email

#     def __repr__(self):
#         return '<User %r>' % self.username

class Blogpost(db.Model):
    id=db.Column(db.Integer, primary_key=True)
    title=db.Column(db.String(50))
    subtitle=db.Column(db.String(50))
    author=db.Column(db.String(50))
    date=db.Column(db.DateTime)
    content=db.Column(db.Text)



@app.route('/')
def index():
    return render_template('index.html')


@app.route('/about')
def about():
    return render_template('about.html')


@app.route('/post')
def post():
    return render_template('post.html')


@app.route('/contact')
def contact():
    return render_template('contact.html')

@app.route('/add')
def add():
    return render_template('add.html')

@app.route('/addpost', methods=['POST'])
def addpost():
    if request.method=='POST':

        title = request.form['title']
        subtitle = request.form['subtitle']
        author = request.form['author']
        date = request.form['date']
        content = request.form['content']
        post=Blogpost(title=title, subtitle=subtitle, author=author, date_posted=datetime.now(), content=content)

       db.session.add(post)
       db.session.commit()

    return redirect(url_for('index'))



if __name__ == '__main__':
    app.run(debug=True)


#And below is my is my add.html file
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Clean Blog - Start Bootstrap Theme</title>

     <!-- Bootstrap core CSS -->
    <link href="{{url_for('static', filename='bootstrap.min.css')}}" rel="stylesheet">

    <!-- Custom fonts for this template -->
    <link href="{{url_for('static', filename='font-awesome.min.css')}}" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

    <!-- Custom styles for this template -->
    <link href="{{url_for('static', filename='clean-blog.min.css')}}" rel="stylesheet">

  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
      <div class="container">
        <a class="navbar-brand" href="{{('/')}}">Start Bootstrap</a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          Menu
          <i class="fa fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link" href="{{('/')}}">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="{{('/about')}}">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="{{('/post')}}">Sample Post</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="{{('/contact')}}">Contact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <!-- Page Header -->
    <header class="masthead" style="background-image: url('{{url_for('static', filename='contact-bg.jpg')}}')">
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="page-heading">
              <h1>Create a new blogpost</h1>
              <span class="subheading">You can create a new blogpost here</span>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- Main Content -->
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">

          <form name="addForm" id="addForm" name="addForm" method="POST" action="{{url_for('addpost')}}" novalidate>
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Title</label>
                <input type="text" class="form-control" placeholder="title" id="title" name="title" required data-validation-required-message="Please enter a title.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Sub-title</label>
                <input type="email" class="form-control" placeholder="subtitle" name="subtitle" id="subtitle" required data-validation-required-message="Please enter your Sub-title.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <div class="control-group">
              <div class="form-group col-xs-12 floating-label-form-group controls">
                <label>Author</label>
                <input type="tel" class="form-control" placeholder="author" name="author" id="author" required data-validation-required-message="Your Name.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
              <div class="control-group">
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Post</label>
                <textarea rows="5" class="form-control" placeholder="content" name="content" id="name" required data-validation-required-message="Please enter content."></textarea>
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <br>
            <div id="success"></div>
            <div class="form-group">
              <button type="submit" class="btn btn-primary" id="sendMessageButton">Send</button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <hr>

    <!-- Footer -->
        <footer>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <ul class="list-inline text-center">
            </ul>
            <p class="copyright text-muted">Copyright &copy; Your Website 2017</p>
          </div>
        </div>
      </div>
    </footer>
    <!-- Bootstrap core JavaScript -->
    <script src="{{url_for('static', filename='jquery.min.js')}}"></script>
    <script src="{{url_for('static', filename='bootstrap.bundle.min.js')}}"></script>

    <!-- Contact Form JavaScript -->
    <script src="{{url_for('static', filename='jqBootstrapValidation.js')}}"></script>
    <script src="{{url_for('static', filename='contact_me.js')}}"></script>



    <!-- Custom scripts for this template -->
    <script src="{{url_for('static', filename='clean-blog.min.js')}}"></script>

  </body>

</html>

当我在填写表单后尝试提交时,会出现以下错误:

错误的请求浏览器(或代理)发送了此服务器无法理解的请求。

我已经试着把它输出到一个文件中,但仍然得到了错误

共有1个答案

白镜
2023-03-14

我用添加调试了这个

import pdb; pdb.set_trace()

addpost()后立即请求方法检查。原来date=request.form['date']指令失败是因为你的表单上没有日期:

(Pdb) request
<Request 'http://localhost:5000/addpost' [POST]>
(Pdb) request.form
ImmutableMultiDict([('content', u'asdfasdfasdfasdf'), ('title', u'asdfasdf'), ('subtitle', u'adsfasdfasdf'), ('author', u'asdfasdfasdf')])
(Pdb) request.form['title']
u'asdfasdf'
(Pdb) request.form['date']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

一旦我删除了date=request.form['date'],错误就消失了。

 类似资料:
  • 我已经为我的生日提醒应用程序创建了一个编辑视图,并且以前工作过,每当我链接到此编辑视图时,我都会收到一个关键错误。我已经检查了get_Gift函数返回的对象,并且键字段存在。此外,使用多个字段创建的HTML表单包含错误中指示的字段。 收到的错误是: werkzeug。例外情况。BadRequestKeyError:400错误请求:浏览器(或代理)发送了此服务器无法理解的请求。KeyError:“g

  • 嗨,我正在编写一个博客脚本,但收到400个错误请求。Python代码: Html代码: 我以前从未收到过这样的错误,我不知道为什么。我猜表格可能有错误。 求你帮帮我

  • 我正在尝试上传文件 错误的请求浏览器(或代理)发送了此服务器无法理解的请求。 超文本标记语言代码

  • 我遇到了表单400错误,并尝试了其他解决方案。它让我的状态有所改善,但并没有阻止错误的发生。我有 模板/注册。html: 形式。py: 这些观点。py: 这些指纹看起来像 就像我预料的那样。我假设根据字段的验证器重新呈现页面,并显示错误消息“Passwords must match”。我该怎么做才能让此表单失败并重新提交页面?非常感谢。

  • 我最近一直在用Java编写自己的Webserver,因为我觉得有一个很不错,昨天我偶然发现了一个问题,我仍然没有解决。我的浏览器(取消搜索Chromium)似乎向服务器发送了一些空请求或类似的东西。我实现了一个请求处理程序,它应该读取GET请求并提取请求的资源。它的工作原理是这样的:它以请求为例:“GET/index.htmlHTTP/1.1”并将其放入带有String.split (" ");

  • 我的一个EC2实例上有一个graphql服务器正在运行。我也有AWS appsync运行,但目前它只与几个Lambda集成。 我想将我的Appsync连接到graphql服务器,这样Appsync将作为特定查询/变化的代理。 因此,从客户端来看,它将如下所示: 客户端将一个查询发送到APPESNC,让我们假设它看起来像这样: Appsync已经定义了一个查询,它被配置为在graphql服务器上代理