Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Flask Framework

You're reading from   Learning Flask Framework Build dynamic, data-driven websites and modern web applications with Flask

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781783983360
Length 250 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Creating Your First Flask Application 2. Relational Databases with SQLAlchemy FREE CHAPTER 3. Templates and Views 4. Forms and Validation 5. Authenticating Users 6. Building an Administrative Dashboard 7. AJAX and RESTful APIs 8. Testing Flask Apps 9. Excellent Extensions 10. Deploying Your Application Index

Restricting access to views


At the moment, all of our blog views are currently unprotected and available to anyone who wants to visit them. In order to prevent a malicious user from trashing our entries, let's add some protection to the views that actually modify data. Flask-Login provides a special decorator login_required that we will use to protect views that should require an authenticated user.

Let's go through the entries blueprint and protect all views that modify data. Start by adding the following import at the top of the blueprint.py module:

from flask.ext.login import login_required

login_required is a decorator, just like app.route, so we will simply wrap the views that we wish to protect. For example, this is how you would protect the image_upload view:

@entries.route('/image-upload/', methods=['GET', 'POST'])
@login_required
def image_upload():
    ...

Go through the module and add the login_required decorator to the following views, taking care to add it below the route decorator...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image