Single context app or app factory with multiple contexts?
rails routes
:
from app_name import app
app.url_map
Common functions in application_controller which load current user to store in session
:
register a function to run before a request through request hooks and store data in g
(context global) dictionary variable.
Render method in a controller action
:
return method in a view function or make_response()
function
Redirect in a controller action :
from flask import redirect
return redirect(http://someotherurl.website.com)
View files (using ERB)
:
Templates (using Jinja2) in templates/
directory, rendered from the view function like this:
return render_template('index.html')
Which passes in variables as additional arguments:
return render_template('index.html', name='Daniel')
And interpolates variables in the template that look like this:
<h1>Hello, {{ name }}!</h1>
Invisible vs. visible ERB tags
:
Interpolated Jinja2 in double braces ({{ variable }}
) or control structures like this: {% if user %}
which are unique to Jinja2 and not pure Python, unlike how ERB allows pure Ruby
Rendering partials in ERB view files
:
Template inheritance using {% block template_name %}
and {% endblock %}
in the root template and {% extends "index.html" %}
in the extending template.
Session access
:
pretty much the same when storing to session, but when retrieving from session use session.get('key_name')
Flash messages
:
In view function, flash('Your flash message)
, and in template, get_flashed_messages()
ActiveRecord : SQLAlchemy
rails db:migrate
:
Within a shell session,
from app import db
db.create_all()
Adding rows to the database:
user = User.new(name: 'Daniel', address: '1251 Dexter St.')
user.save
:
user = User(name='Daniel', address='1251 Dexter St.')
db.session.add(user)
db.session.commit
User.all
:
User.query.all()
User.where(name: 'Daniel')
:
User.query.filter_by(name='Daniel').all()
User.where(name: 'Daniel').first
:
User.query.filter_by(name='Daniel').first()
User.all.to_sql
:
str(User.query.all())
SQLAlchemy "query filters" (from "Flask Web Development," Ch. 5, table 5-5):
filter()
Returns a new query that adds an additional filter to the original queryfilter_by()
Returns a new query that adds an additional equality filter to the original querylimit()
Returns a new query that limits the number of results of the original query to the given numberoffset()
Returns a new query that applies an offset into the list of results of the original queryorder_by()
Returns a new query that sorts the results of the original query according to the given criteriagroup_by()
Returns a new query that groups the results of the original query according to the given criteria
SQLAlchemy "query executors" (from "Flask Web Development", Ch. 5, table 5-6):
all()
Returns all the results of a query as a listfirst()
Returns the first result of a query, or None if there are no resultsfirst_or_404()
Returns the first result of a query, or aborts the request and sends a 404 error as response if there are no resultsget()
Returns the row that matches the given primary key, or None if no matching row is foundget_or_404()
Returns the row that matches the given primary key. If the key is not found it aborts the request and sends a 404 error as responsecount()
Returns the result count of the querypaginate()
Returns a Pagination object that contains the specified range of results