Home > OS >  Flask - Form Data is None?
Flask - Form Data is None?

Time:12-23

I am attempting to setup hard coded authentication just for testing in my def login() route but forms.user_email.data/forms.user_password.data is returning None? I did also notice that form in my def home() is exhibiting the same behavior. I believe this is the root of multiple issues, but I'm honestly not sure. I have read multiple posts about using request - but I've been seeing other people use FlaskForm to achieve this?

Any guidance is greatly appreciated, and any general Flask tips as well - I am quite new to Python/Flask. I have been having a hard time understanding best practices/use-cases for flask/flask_restful, not to mention all the possible extensions like flask_bootstrap, FlaskForm, WTForms, FlaskLogin etc. Thanks in advance.


from flask import Flask, render_template, url_for, redirect, flash
from flask_restful import Resource, Api
from flask_wtf import FlaskForm
import requests
import pandas as pd
from flask_bootstrap import Bootstrap
from wtforms import EmailField, PasswordField, StringField
from wtforms import validators



app = Flask(__name__)
api = Api(app)
app.config['SECRET_KEY'] = 'd43b80c0727dca296c607fe8f8db478264'
bootstrap = Bootstrap(app)

class LoginForm(FlaskForm):
    user_email = EmailField('Email', validators=[validators.DataRequired()])
    user_password = PasswordField('Password', validators=[validators.DataRequired()])


class InputForm(FlaskForm):
    ticker = StringField('Ticker', validators=[validators.DataRequired()])



class GetStonks(Resource):
    def get(self, ticker):

        HEADER = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"}

        try:
            response = requests.get(f"https://finviz.com/quote.ashx?t={ticker}", headers=HEADER)
            result = pd.read_html(response.text)
            parsed = result[5]

            dict_primary = {}

            dict_primary |= dict(parsed[[0,1]].values)
            dict_primary |= dict(parsed[[2,3]].values)
            dict_primary |= dict(parsed[[4,5]].values)
            dict_primary |= dict(parsed[[6,7]].values)
            dict_primary |= dict(parsed[[8,9]].values)
            dict_primary |= dict(parsed[[10,11]].values)


            return dict_primary, 200
        except IndexError:
            return {"Message": "Ticker ID not found."}

api.add_resource(GetStonks, "/api/v1/stocks/<string:ticker>")

@app.route('/')
@app.route("/login", methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.user_email.data == '[email protected]' and form.user_password.data == 'admin':
            return redirect(url_for('home'))
        else:
            flash('Authentication failure', 'danger')

    return render_template('login.html', title='Login', form=form)


@app.route("/home", methods=['GET', 'POST'])
def home():
    form = InputForm()
    return render_template('home.html', title='Home', form=form)


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

home.html

{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}

{% block title %}
Home
{% endblock %}

{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">

{% block body %}
<div >
  <form  method="POST" action="http://127.0.0.1:5000/api/v1/stocks/{{url}}">
    <h2 >INPUT TICKER BELOW</h2>
    <br>
    {{ form.hidden_tag() }}
    {{ form.csrf_token }}
    {{ wtf.form_field(form.ticker) }}
    <button  type="submit">GET</button>
  </form>
</div>
{% endblock %}

login.html

{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %}

{% block title %}
Login
{% endblock %}

{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='signin.css')}}">

{% block content %}
    <div >
      <form  method="POST" action="{{ url_for('home') }}">
        <h2 >SIMPLE STOCK API</h2>
        <br>
        <br>
        {{ form.hidden_tag() }}
        {{ form.csrf_token }}
        {{ wtf.form_field(form.user_email) }}
        {{ wtf.form_field(form.user_password) }}
        <button  type="submit">Sign in</button>
      </form>
    </div>
{% endblock %}

CodePudding user response:

Inside your login.html instead of:

<form  method="POST" action="{{ url_for('home') }}">

Action should be login instead of home. So it should look like this:

<form  method="POST" action="{{ url_for('login') }}">
  • Related