URGENT help required: WebApplication development with Flask, module and module object issues

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:
    return render_template('home.html', todos = todos, template_form = TodosForm())

The HTML file:

<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <h1>My todo list</h1>

    {% for todo in todos %}
        <li>{{ todo }}</li>
    {% endfor %}

    <form action="POST">
        {{ template_form.hidden_tag() }}

            {{ template_form.todo.label }}
            {{ template_form.todo() }}

        <p> {{ template_form.submit() }}</p>

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…

Is your home.html in a templates folder (and is it named home.html, I know this is silly, but maybe you named it index.html or something)? Is the templates folder in the correct directory?

Definitely. Directory and folders are indeed correct. I have actually solved the first problem - it was a very silly mistake: it should read ‘method’ and not ‘action’ in HTML file… Thank you for taking time to help me - really appreciate it!

Can you describe the sqlalchemy problem some more?

Is it installed in the venv, did you install flask-sqlalchemy and not sqlalchemy, are you importing flask-sqlalchemy and not sqlalchemy, sometimes I use both so that can get mixed up.

What’s the error message saying, for example (copy and paste)?

I am thinking that I may have problems with importing flask_sqlalchemy because my actual SQLite is globally installed and it is not installed in the environment. But… I can create an SQLite database inside the environment… I will go and try to install SQLite locally - will update you on the outcome.

I don’t think sqlite would encroach on sqlalchemy importing, it would be a problem at another point if the issue was there.

Error: ’ there is no ‘flask_sqlalchemy’ module’

I have installed literally all types of flask_sqlalchemy. I installed SQLAlchemy, flask_sqlalchemy, Flask_SQLAlckemy, sqlalchemy and the list goes on.

The problem I am facing is just that I cannot access the flask_sqlalchemy module. That is the entire problem.

Try reading this thread to see if this is your case: https://www.reddit.com/r/flask/comments/9r0kmz/python_37_error_with_flasksqlalchemy/ This line jumped as maybe relevant:

What also might be happening is that you successfully install flask-sqlalchemy to your venv and then use the system python (not venv) to try to import it.

some code to verify that it imports:

import importlib as imp
    found = True
except ImportError:
    found = False
1 Like

@toastedpitabread Thank you so much! Indeed, that solved the problem. Apparently I mixed using system and environment python and pip. Thank you so much! Really appreciate your help! And it was very quick - you replied within an hour after posting. Once again, thank you so much!

No problem! Happy to help! Flask can be a little daunting at first… and at second. But when one gets used to it it’s great.

@toastedpitabread I am sure that Flask is great) Is there a chance we can connect on GitHub to collaborate on a project in about a week, or so? Also, let me know if I can somehow award you a 5-star feedback on the forum. Thank you!

@moralez Miguel Grinberg and his yt + book and Corey Schaffer’s yt gave me a lot of good reference points starting out. If you’re interested. Grinberg is a little drier but it’s very solid stuff.

Haha no reviews need! This is just volunteer. I’m just another learner.

Certainly, will check those out! Once again, thank you!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.