I was doing ‘Develop Web apps with Python using Flask’ path. And I ran into two problems. Firstly, my Flask forms do not work the way they are supposed to - after entering any text and submitting the form on my website, the client gets thrown to a random url and it says ‘there is no such path’. I almost reproduced code provided in the lessons so there should not be any problems. The link to the lesson: https://youtu.be/O0gcAh-cRvw
Please see my code below: the app.py is the first codeblock and the html template is the second one. The html template is inside the templates folder. The project folder in its own virtual environment.
from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
todos = ['work', 'sleep', 'eat']
class TodosForm(FlaskForm):
todo = StringField('Todo')
submit = SubmitField('Add todo')
@app.route('/', methods=['GET','POST'])
def home():
if 'todo' in request.form:
todos.append(request.form['todo'])
return render_template('home.html', todos = todos, template_form = TodosForm())
The HTML file:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>My todo list</h1>
{% for todo in todos %}
<li>{{ todo }}</li>
{% endfor %}
<form action="POST">
{{ template_form.hidden_tag() }}
<p>
{{ template_form.todo.label }}
{{ template_form.todo() }}
</p>
<p> {{ template_form.submit() }}</p>
</form>
</body>
</html>
The second problem is that somehow after installing flask_sqlalchemy I cannot access it. It just keeps saying there is no such module but I see it with ‘pip list’ command. I do not know what to do. This question is isolated and doesn’t have anything to do with the code above. Again I reproduced the code in this lesson: https://youtu.be/O-eiYcMWD30 However, because I cannot use that module I don’t have my database and thus nothing is works properly.
I would appreciate any help sorting this mess out. I do not understand where I have made the mistake. And it is very frustrating as I am doing everything as shown but it does not work on my machine…