🌍 Python · flashcards
Python Flask Blog Project Flashcards
71 question-and-answer cards covering Flask Blog Project as it is examined in Python. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.
24 sample cards from the Flask Blog Project deck
Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.
How do you fetch a record or return 404 if it doesn't exist in Flask-SQLAlchemy?
Use Model.query.get_or_404(id), e.g. Post.query.get_or_404(post_id). It returns the row or aborts with a 404 Not Found.
How do you insert a new record (e.g. a Post) into the database in SQLAlchemy?
Create the object, then db.session.add(post) followed by db.session.commit(). add stages it; commit writes it to the database.
What is the role of url_for() in Flask routing?
It builds a URL for a given endpoint name (e.g. url_for('post', post_id=3)), avoiding hardcoded paths so links stay correct if routes change. Often used for redirects and template links.
What templating engine does Flask use, and what file extension do templates typically have?
Flask uses the Jinja2 templating engine, with templates stored in a 'templates/' folder and commonly using the .html extension.
What is the difference between Jinja2's {{ }} and {% %} delimiters?
{{ ... }} outputs/evaluates an expression (prints a value). {% ... %} runs a statement/control structure such as for-loops, if-conditions, and block/extends tags.
How does Jinja2 template inheritance work with base templates?
A base template defines {% block content %}{% endblock %} regions; child templates start with {% extends 'base.html' %} and override blocks with {% block content %}...{% endblock %}, reusing shared layout.
How do you loop over a list of posts in a Jinja2 template?
{% for post in posts %} ... {{ post.title }} ... {% endfor %}. The posts variable is passed from the view via render_template('home.html', posts=posts).
What is autoescaping in Jinja2 and why does it matter for a blog?
Jinja2 automatically escapes HTML in {{ }} output, converting characters like < and & to entities. This prevents XSS attacks from user-submitted content such as comments and post bodies.
Write the core fields of a Comment model linked to both a Post and a User.
class Comment(db.Model):\n id = db.Column(db.Integer, primary_key=True)\n content = db.Column(db.Text, nullable=False)\n date_posted = db.Column(db.DateTime, default=datetime.utcnow)\n post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)\n user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
How do you model the one-to-many relationship between Post and Comment?
On Post: comments = db.relationship('Comment', backref='post', lazy=True). One post has many comments, and each comment's foreign key post_id references post.id.
Why do comments typically have two foreign keys in a blog schema?
A comment belongs to a specific post (post_id) and is authored by a specific user (user_id), so it needs foreign keys to both the Post and User tables.
How would you design a route that adds a comment to a specific post?
@app.route('/post/<int:post_id>/comment', methods=['POST']) — validate the form, create Comment(content=..., post_id=post_id, user_id=current_user.id), db.session.add + commit, then redirect back to the post.
How do you require login before allowing a user to submit a comment?
Decorate the comment route with @login_required so only authenticated users can post, and use current_user.id to attribute the comment to the logged-in user.
How are existing comments displayed within a single post's template?
Loop over the post's related comments: {% for comment in post.comments %} {{ comment.content }} by {{ comment.author.username }} {% endfor %}, accessing them via the backref relationship.
After redirecting from a successful comment submission, how do you confirm success to the user?
Use Flask's flash('Comment added!') before redirect, and render flashed messages in the template via get_flashed_messages(), giving user feedback under the Post/Redirect/Get pattern.
What is the Post/Redirect/Get (PRG) pattern and why use it after form submission?
After a successful POST, the server issues a redirect so the browser does a fresh GET. This prevents duplicate submissions when the user refreshes the page (e.g. double-posting a comment).
What is a WSGI server and why is the Flask built-in server not used in production?
WSGI (Web Server Gateway Interface) is the standard between Python web apps and servers. Flask's dev server is single-threaded and insecure; production uses WSGI servers like Gunicorn or uWSGI for performance and stability.
Name common hosting/deployment options for a Flask blog application.
Platform-as-a-Service like Heroku, Render, or PythonAnywhere; cloud providers like AWS (Elastic Beanstalk/EC2), Google Cloud, or Azure; often fronted by Nginx with a Gunicorn WSGI server.
What factors should guide choosing a hosting service for a Flask app?
Cost/free tier, ease of deployment, scalability, supported database options, custom domain and HTTPS support, and built-in CI/CD. Beginners often favor PaaS (Render, PythonAnywhere) for simplicity.
What is Gunicorn and how is a Flask app typically launched with it?
Gunicorn ('Green Unicorn') is a production WSGI HTTP server. Launch with: gunicorn app:app (module:Flask-instance), often configuring multiple worker processes for concurrency.
What is a Procfile and what does it specify for PaaS deployment?
A Procfile (used by Heroku/Render) declares the process types and commands to run, e.g. 'web: gunicorn app:app', telling the platform how to start the web server.
Why must DEBUG be disabled and SECRET_KEY be set securely before deploying to production?
Debug mode exposes an interactive console and stack traces enabling remote code execution. A weak/known SECRET_KEY lets attackers forge sessions and CSRF tokens. Both are critical production security settings.
How should secrets like SECRET_KEY and database URIs be supplied in production rather than hardcoding?
Read them from environment variables, e.g. SECRET_KEY = os.environ.get('SECRET_KEY'), keeping secrets out of source control and allowing per-environment configuration.
What is the recommended production database choice over SQLite for a deployed Flask blog, and why?
PostgreSQL (or MySQL). SQLite is file-based and poor for concurrent writes and multi-process servers, while PostgreSQL handles concurrency, scaling, and remote connections needed in production.
What this deck covers
The Flask Blog Project deck follows the Python Flask Blog Project syllabus — 7 chapters and 20 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 10.1 cards per chapter.
Answers are written to be recallable, not just readable — averaging about 182 characters, which is long enough to carry the reasoning and short enough to say out loud.
A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.
Flask Blog Project flashcards FAQ
How many Flask Blog Project flashcards are in this Python deck?
71 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.
Are these Python flashcards free?
Yes. The preview here is free to read with no signup, and the full 71-card deck is free inside the Examius app.
What do the Flask Blog Project cards cover?
They follow the Python Flask Blog Project syllabus — 7 chapters and 20 topics — so the questions track what is actually examinable.
How should I use these flashcards?
Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.