-1

I have been following a pretty standard intro to python and flask, I'm new to flask but have a few months coding python.

The error I get is:

File "C:/Users/danny/PycharmProjects/learning_flask/venv/hello.py", line 26, in login session['username'] = request.form.get['username'] TypeError: 'method' object is not subscriptable 127.0.0.1 - - [11/May/2019 16:45:43] "POST /login HTTP/1.1" 500 -

I had this working, I think I made some changes to the templates and thats when it stopped working. I've looked through stacktrace and other sources for errors, itseemsto be saying there is a problem in the login @app.route (but not helped by course where code and templates work every time! If only the showed you some of the errors, if theres a site that does great - would love url).

import os

from flask import Flask, render_template, request, redirect, url_for, flash, make_response, session

app=Flask(__name__)
app.secret_key = "SuperSecretKey"

@app.route('/login', methods=['GET','POST'])
def login():
    error = None
    if request.method == 'POST':
    if valid_login(request.form['username'], request.form['password']):
        flash('Sucessfully logged in')
        session['username'] = request.form.get['username']
        return redirect(url_for('welcome'))
        # return redirect(url_for('help'))
    else:
        error = "There was an error with either the username or password"
        return render_template('login.html', error=error)

@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))


@app.route('/')
def welcome():
    if username in session:
        return render_template('welcome.html', 
        username=session['username'])
    else:
        return redirect(url_for('login'))

def valid_login(username, password):
    if username == password:
        return True
    else:
        return False

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

should load login page, but get server not found, and if successful allow me to get to welcome page, templates below I think they are ok tho'

Base.html

<head>
    {% block head %}
    <title>{% block title %}{% endblock %}</title>
    {% endblock %}
</head>

<body>
<h1>Lets Scamperdoodle</h1>

{% with messages = get_flashed_messages() %}

{% if messages %}
    <ul class="flashes">
        {% for message in messages %}
        <li>{{ message }}</li>
        {% endfor %}
    </ul>
{% endif %}

{% endwith %}

<div id="content">
    {% block content %}{% endblock %}
</div>
</body>

<footer>
    {% block footer %}
    &copy; Copyright 2019 <a href="http://scamperdoodle.co.uk"> Scamperdoodle</a>
    {% endblock %}
</footer>

login.html

{% extends "base.html" %}

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

{% block content %}

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

    {% if error %}
    <p style="color:red"> {{ error }} </p>
    {% endif %}

    <p>
        Username: <input type="text" name="username" />
    </p>

    <p>
        Password: <input type="password" name="password" />
    </p>

    <button type="Submit">Login</button>
</form>

{% endblock %}
R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
dannyhr9
  • 1
  • 2
  • The indentation in `login()` is incorrect – roganjosh May 11 '19 at 16:19
  • `if username == password` makes no sense. Why would you expect this condition? – roganjosh May 11 '19 at 16:20
  • blimey that was quick!!! thank you so much!! I will give suggestions a go. I think I can explain roganhosh's comment my fault I kept old code in from the course but must have missed this line oops! lesson for next time. – dannyhr9 May 11 '19 at 16:24
  • I've made changes suggested but still get problem – dannyhr9 May 11 '19 at 16:33
  • snakecharmerb fixed the error and I pointed out another. It's not clear what your new issue is, but SO doesn't work well chasing new errors – roganjosh May 11 '19 at 16:38
  • I have made adjustment but now I'm getting following error that I dont understand File "C:\Users\danny\PycharmProjects\learning_flask\venv\lib\site-packages\flask\templating.py", line 86, in _get_source_fast raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: login.html 127.0.0.1 - - [11/May/2019 18:11:08] "GET /login HTTP/1.1" 500 - – dannyhr9 May 11 '19 at 17:09

1 Answers1

0

The error is very readable and useful:

TypeError: 'method' object is not subscriptable

'Not subscriptable' means your are calling [keyword] on an object that does not support it. The error even tells you the object is a 'method' i.e .get is the method you called before the subscription.

Repeating comment made (should have been be posted as answer):

request.form.get['username'] should be either request.form['username'] or request.form.get('username')

Andrew Allen
  • 6,512
  • 5
  • 30
  • 73