Issue
I'm a student, I'm trying to create an authorization page on the site. I want to link MySQL python to an html page, I found instructions on the Internet (link: article), but I came across the error presented above, it occurs when I click on the "sign up" button, communication with GPT did not give me anything(. If necessary, I can attach the zip archive itself with all the files.
all files are readable, flask writes 304, but when I click on the button it gives 400 - I found out that this is a data error, but it doesn't give me anything, I'll glue the code from below.
python
from flask import Flask, render_template, request, json
app = Flask(__name__)
@app.route("/")
def main():
return render_template('signup.html')
@app.route('/signUp', methods=['POST'])
def signUp():
print(request.form)
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
if _name and _email and _password:
print("да")
return json.dumps({'html':'<span>Все данные записаны !!!</span>'})
else:
print("нет")
return json.dumps({'html':'<span>Вы что то пропустили :(</span>'})
if __name__ == "__main__":
app.run()
html
<DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>СООП</title>
<script src="/static/js/jquery-3.7.1.js"></script>
</head>
<body>
<h4 align="middle">Авторизация</h4>
<p align="middle">
<label for="inputName">Name</label>
<input type="name" name="inputName" id="inputName" placeholder="Name" required autofocus> <br></br>
<label for="inputEmail">Email address</label>
<input type="email" name="inputEmail" id="inputEmail" placeholder="Email address" required autofocus> <br></br>
<label for=inputPassword">Password</label>
<input type="password" name="inputPassword" id="inputPassword" placeholder="Password" required> <br></br>
<button id="btnSignUp" type="button">Sign in</button>
</p>
<script src="/static/js/btn.js"></script>
</body>
</html>
I will be grateful for any help, I hope I described it in detail
if you believe the description, it should show that the data is filled in or not filled in, depending on the availability of data in the variables
There is an assumption that I did not do the ajax scripts correctly, because the article does not specify how to save and where, only the root path where to save Jquery is written and the script for the button is written
P.S. Here is the full set of Flask text: [31m[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.[0m
- Running on http://127.0.0.1:5000 [33mPress CTRL+C to quit[0m 127.0.0.1 - - [14/Jan/2024 14:14:58] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [14/Jan/2024 14:14:58] "[36mGET /static/js/jquery-3.7.1.js HTTP/1.1[0m" 304 - 127.0.0.1 - - [14/Jan/2024 14:14:58] "[36mGET /static/js/btn.js HTTP/1.1[0m" 304 - ImmutableMultiDict([]) 127.0.0.1 - - [14/Jan/2024 14:32:45] "[31m[1mPOST /signUp HTTP/1.1[0m" 400 -
Solution
You are trying to send a form without sending it, your code missing a lot of things. I will suggest you to learn a little bit about flask.
Let's break down the problem:
- Missing form on your html file.
- Missing
url_for()
to handle the submission of the form - Not handling the button click event to send the form.
Fix:
Make your inputs, form submitting button inside the <form>
tag, and add action = "{{ url_for('signUp')"}}
and make the form method post
:
<form id="signup_form" action="{{url_for('signUp')}}" method="post">
<label for="inputName">Name</label>
<input type="name" name="inputName" id="inputName" placeholder="Name" required autofocus> <br></br>
<label for="inputEmail">Email address</label>
<input type="email" name="inputEmail" id="inputEmail" placeholder="Email address" required autofocus> <br></br>
<label for="inputPassword">Password</label>
<input type="password" name="inputPassword" id="inputPassword" placeholder="Password" required> <br></br>
<button id="btnSignUp" type="submit">Sign in</button>
</form>
Also you should add this code to be able to send the form (since you are using button
not input<type = "button">
which would've made the form easier to send.
<script>
send = document.getElementById("#btnSignUp")
form = document.getElementById("#signup_form")
send.onclick(form.send())
</script>
Answered By - Mahmoud Dello
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.