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 %}
© 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 %}