Flask
-
I'm looking at Flask on the example of a simple user name and password page. The code does not work or return the value.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Вход</title> <style> h2 { background-color: gray; color: black; padding: 10px; margin-top: 0px; } div { margit-top: 50% } </style> </head> <body> <h2>Автосервис</h2>
{% if message %}
<p>{{message}}</p>
{% endif %}<form action="" method="post">
<label for="username">Username</label>
<input type = "text" name = "username">
<br>
<label for="password">Password</label>
<input type = "password" name = "password">
<br>
<input type="submit" value="Вход"
</form></body>
</html>
Code Python:
@app.route('/login', methods = ['post', 'get'])
def login():
message = ''
if request.method == 'post':
username = request.form.get('username')
password = request.form.get('password')
if username == 'root' and password == 'root':
message = 'correct'
else:
message = 'incorrent'
return render_template('login.html', message = message)
-
I advise you to take good care of yourself and study. https://flask-wtf.readthedocs.io/en/v0.8.3/ https://habr.com/ru/post/346342/ (Continuing to read)
FlaskForm creates forms as objects for further transfer of the html file and processing on the server ' s side.
I advise you in detail on the documentation and, if necessary, look at the example of my recent project.
This is an example of the shape of my project.
from flask_wtf import FlaskForm from wtforms import PasswordField, StringField, SubmitField, TextAreaField, BooleanField, IntegerField from wtforms.validators import DataRequired, EqualTo, Email, Length
class RegistrationForm(FlaskForm):
name = StringField("Your name", validators=[DataRequired()])
email = StringField("E-mail", validators=[DataRequired(), Email('Incorrect email')])
password = PasswordField("Password", validators=[DataRequired()])
confirm_password = PasswordField("Confirm password", validators=[
DataRequired(), EqualTo("password", message="Passwords must match")])
description = TextAreaField("Short description (you can fill it later)")
submit = SubmitField("Let's go!")
This form of the html page should transmitted further:
form = RegistrationForm()
return render_template("registration.html", form=form)
In html (as an example):
<form method="post" class="main-registration-form">
{{ form.hidden_tag() }}
<div class="form-group main-form-group">
{{ form.name(id="name", class='main-registration-input', placeholder='Login') }}
</div>
<div class="form-group main-form-group">
{{ form.email(id="email", class='main-registration-input', placeholder="Email") }}
{% for error in form.email.errors %}
<span class="main-registration-error">{{ error }}</span>
{% endfor %}
</div>
<div class="form-group main-form-group">
{{ form.password(id="password", class='main-registration-input', placeholder="Password") }}
</div>
<div class="form-group main-form-group">
{{ form.confirm_password(id="confirm_password", class='main-registration-input', placeholder="Confirm password") }}
{% for error in form.confirm_password.errors %}
<span class="main-registration-error">{{ error }}</span>
{% endfor %}
</div>
<div class="form-group main-form-group">
{{ form.description(type="textarea", id="description", class='py-2 main-registration-input main-registration-description', placeholder="Description") }}
</div>
{{ form.submit(class='main-registration-button') }}
</form>
Then we'll get this form on the server's side of the same page rib function.
form = RegistrationForm()
We've already written (see above)
Now add the form data processing as follows:if form.validate_on_submit():
// code
How could you know from the function, she's checking the shape's eval.
At the end, if you need to write a new one.render_template
orredirect