Empty flask

I am trying to create a simple form created in Flask for my own education. I have a login.html page with this form code:

<form action="{{ url_for('login') }}" method="post"> <div> <label for="username">Username</label> <div> <input type="text" id="username" name="username" placeholder="Username"> </div> </div> <div> <label for="password">Password</label> <div> <input type="password" id="password" name="password" placeholder="Password"> </div> </div> <div > <input class="btn" type="submit"> </div> </form> 

I use the following code to get this code: Flask returns an empty request.form , so I cannot process it.

 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': request.form['username'] ... 

I really don't want to study another library (WTForms) right now, and I use bootstrap to add to the headache. What I do not see here with Flask / HTML?

+4
source share
3 answers

I had this problem, but it was because I forgot to assign a name attribute to my input elements, and I tried to access form data using the id attribute instead

i.e.

My HTML and Python were as below

HTML

 <input type="text" id="usernameTxtBx"> 

Python

 request.form['usernameTxtBx'] 

What i did now:

HTML

 <input type="text" name="username" id="usernameTxtBx"> 

Python

 request.form['username'] 

I also needed to make sure I used the POST request. The GET gave me an empty dictionary in my python code.

The OP did not make any of these errors. But it can help someone who stumbled upon this topic.

+6
source

I had this problem. Some tools, such as the postman or some libraries or a web browser, send data in such a way that the jar does not identify as published values. From my point of view, this is a problem with the bulb.

This is the workaround that I used to solve it: 1 - I sent the information using json. Look at this:

How to send a JSON object using html form data

2 - I instead get the parameters using: value = request.form ["myparamname"] I used this:

 json_data = request.get_json(force=True) value = json_data["myparamname"] 
+2
source

login.html

 {% extends "layout.html" %} {% block content %} <div class="form"> <h2>Sign In</h2> {% for field in form.errors %} {% for error in form.errors[field] %} <div class="alert alert-error"> <strong>Oops...</strong> {{error}}. </div> {% endfor %} {% endfor %} <form action="{{ url_for('login') }}" method=post> {{ form.hidden_tag() }} {{ form.email.label }} {{ form.email }} {{ form.password.label }} {{ form.password }} <p> <input id="submit" name="submit" type="submit" class="btn btn-inverse" value="Sign In"></p> </form> </div> {% endblock %} 

forms.py

  class SigninForm(Form): email = TextField("email", [validators.Required("Please enter your email")]) password = PasswordField('Password', [validators.Required("Please enter a password.")]) submit = SubmitField("Sign In") 

Then import signinform into your views and create your login method like this

 @app.route('/login', methods=['GET', 'POST']) def signin(): form = SigninForm() if form.validate_on_submit(): session['email'] = form.email.data flash('You are logged in') return redirect(url_for('dashboard')) return render_template('signin.html', form=form) 

Refer to this guide for more detailed instructions http://pypix.com/python/building-flask-blog-part-1/

-2
source

All Articles