-
Notifications
You must be signed in to change notification settings - Fork 0
Home
I'm using this repo just to learn Flask web framework, building a blog step by step following The Flask Mega-Tutorial by Miguel Grinberg.
While studying a technology I like to document the topic I'm learning. This wiki will help me to fix the concepts 😉
The Quickstart Flask on Ubuntu shows how to start with a basic but working project.
As reported in many tutorials there are two ways to deliver a Flask project:
- module: good for small projects (even a single file project)
- package: good for bigger project so that you can separate routing, templates, database, users... into separated files and folders.
The "package way" is the chosen one for this microblog project.
microblog/
| venv/
| templates/
| | index.html
| app/
| | __init__.py
| | routes.py
| microblog.py
| .flaskenv
- Run these commands into the shell:
cd microblog source env/bin/activate flask run
- Click on this link http://127.0.0.1:5000/
It's possible that you would like to see how the website looks on smartphones. If you are using a trusted local network (remember this project is running in development mode which is unsafe!!), then the --host
option can be add to the flask run
command:
flask run --host=0.0.0.0
This makes the website on your computer visible in the local network.
For instance, if your IP is 192.168.0.5
, the website will be reached at this address: http://192.168.0.5:5000/
- To stop the server press [Ctrl + C]
- To exit from virtual environment ran the command:
deactivate
- Every time you start a session you should export the FLASK_APP variable, running
export FLASK_APP=microblog.py
.
- Every time you change a file you should stop and restart the server.
Annoying? Yes!
Microblog/.flaskenv
file makes things easier:
- Install python-dotenv package:
pip install python-dotenv
- Create the Microblog/.flaskenv file with the following strings inside:
FLASK_APP=microblog.py FLASK_ENV=development
‼️ the development option is not meant to be used in production.
Now start the server just typing flask run
and after you have changed some files, just refresh the page to see the result!
Sometimes it can be useful to run the Python shell in virtual environment typing:
flask shell
... but it can be annoying to load every time the same modules at the beginning of the session.
Fortunately it's possible to configure microblog.py so that you'll find modules ready to use. In this case the file has been configured to load useful stuff related to the database:
from app import app, db
from app.models import User, Post
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Post': Post}
See the pages:
-
Login no db:
- flash message for no validation;
- flash welcome message at validation on index.html.
-
Login with db:
- managing simple login (registration by flask shell).
-
Login and registration:
- Registration form integrated with db.
-
Login required:
- Homepage visible only after login.
pip install flask-sqlalchemy flask-migrate
See the pages:
- Create database:
flask db init
- The following commands need to be run after changing models.py.
flask db migrate -m "type a message" flask db upgrade
- In case of problems you can go back running:
flask db downgrade
- Create user with username and email:
u = User(username='sara', email='sara@mail.com') # create user
- Save (or not) data into db and delete it:
db.session.add(u) # add user 'u' to db (only for this session!) db.session.rollback() # cancel changes db.session.commit() # make changes permanent db.session.delete(u) # delete user 'u'
- Queries:
User.query.all() # Create list with all users User.query.get(1) # return user with ID = 1
See page: Password