Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Django Design Patterns and Best Practices

You're reading from   Django Design Patterns and Best Practices Easily build maintainable websites with powerful and relevant Django design patterns

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781783986644
Length 222 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Arun Ravindran Arun Ravindran
Author Profile Icon Arun Ravindran
Arun Ravindran
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Django and Patterns 2. Application Design FREE CHAPTER 3. Models 4. Views and URLs 5. Templates 6. Admin Interface 7. Forms 8. Dealing with Legacy Code 9. Testing and Debugging 10. Security 11. Production-ready A. Python 2 versus Python 3 Index

Decorators


Before class-based views, decorators were the only way to change the behavior of function-based views. Being wrappers around a function, they cannot change the inner working of the view, and thus effectively treat them as black boxes.

A decorator is function that takes a function and returns the decorated function. Confused? There is some syntactic sugar to help you. Use the annotation notation @, as shown in the following login_required decorator example:

@login_required
def simple_view(request):
    return HttpResponse()

The following code is exactly same as above:

def simple_view(request):
    return HttpResponse()

simple_view = login_required(simple_view)

Since login_required wraps around the view, a wrapper function gets the control first. If the user was not logged in, then it redirects to settings.LOGIN_URL. Otherwise, it executes simple_view as if it did not exist.

Decorators are less flexible than mixins. However, they are simpler. You can use both decorators and mixins in...

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