Hi pafk, thanks you for your continous supports. I re-assembled these
codes into another account, and I got the expected result from return
(request.form)
. The out put displayed in return(request.form)
for
what I typed into the register.html
when I hit "submit". However,
there is an errorlog when I commented the return (request.form)
. The
keyerror addressed in the "contents" block of 'index' view in
flask_app.py
, instead of in the register
view. I didn't know why
errorlog pointed keyerror: "contents" to line 41
"dbs.sesion.add(comment)"
flask_app.py code:
class Comment(dbs.Model):
__tablename__ = "comments1"
id = dbs.Column(dbs.Integer, primary_key=True)
content = dbs.Column(dbs.String(4096))
class User(dbs.Model):
__tablename__ = "users"
id = dbs.Column(dbs.Integer, primary_key=True)
username = dbs.Column(dbs.String(128))
password_hash = dbs.Column(dbs.String(128))
comments = []
username = []
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
#return render_template("main_page.html", comments=comments) #data is not save to mysql
return render_template("main_page.html", comments=Comment.query.all())#data is queried to mysql
#return (request.form)
comment = Comment(content=request.form["contents"])
dbs.session.add(comment)
dbs.session.commit()
return redirect(url_for('index'))
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method =="GET":
#return render_template("register.html", username=username)
return render_template("register.html", username=User.query.all())
#return (request.form)#ok return something
username1 = User(username=request.form["username"])
dbs.session.add(username1) #5
dbs.session.commit()
return redirect(url_for("register.html"))
main_page.html code:
<html>
<head>
<title>My scratchboard page</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#">My scratchpad</a>
</div>
</div>
</nav>
<div class="container">
{% for comment in comments %}
<div class="row">
{{ comment.content }}
</div>
{% endfor %}
<div class="row">
<form action="." method="POST">
<textarea class="form-control" name="contents" placeholder="Enter a comment"></textarea>
<input type="submit" class="btn btn-success" value="Post comment">
</form>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="{{ url_for('register') }}">Register</a></li>
</ul>
</div><!-- /.container -->
</body>
</html>
register.html code:
<html>
<head>
<title>My scratchboard page</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#">My scratchpad</a>
</div>
</div>
</nav>
<div class="container">
{% for username1 in username %}
<div class="row">
{{ username1.username }}
</div>
{% endfor %}
<div class="row">
<form action="." method="POST">
<textarea class="form-control" name="username" placeholder="Enter a user"></textarea>
<input type="submit" class="btn btn-success" value="Post user">
</form>
</div>
</div><!-- /.container -->
</body>
</html>
errorlog:
2022-03-21 06:36:08,101: Error running WSGI application
2022-03-21 06:36:08,102: werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
2022-03-21 06:36:08,103: KeyError: 'contents'
2022-03-21 06:36:08,103: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2069, in __call__
2022-03-21 06:36:08,103: return self.wsgi_app(environ, start_response)
2022-03-21 06:36:08,103:
2022-03-21 06:36:08,103: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2054, in wsgi_app
2022-03-21 06:36:08,103: response = self.handle_exception(e)
2022-03-21 06:36:08,104:
2022-03-21 06:36:08,104: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2051, in wsgi_app
2022-03-21 06:36:08,104: response = self.full_dispatch_request()
2022-03-21 06:36:08,104:
2022-03-21 06:36:08,104: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1501, in full_dispatch_request
2022-03-21 06:36:08,104: rv = self.handle_user_exception(e)
2022-03-21 06:36:08,104:
2022-03-21 06:36:08,105: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1499, in full_dispatch_request
2022-03-21 06:36:08,105: rv = self.dispatch_request()
2022-03-21 06:36:08,105:
2022-03-21 06:36:08,105: File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1485, in dispatch_request 2022-03-21 06:36:08,105: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2022-03-21 06:36:08,105:
2022-03-21 06:36:08,106: File "/home/kamusg/mysite/flask_app.py", line 41, in index
2022-03-21 06:36:08,106: dbs.session.add(comment)
2022-03-21 06:36:08,106:
2022-03-21 06:36:08,106: File "/usr/local/lib/python3.6/site-packages/werkzeug/datastructures.py", line 377, in __getitem__
2022-03-21 06:36:08,106: raise exceptions.BadRequestKeyError(key)