Book Image

Mastering Django: Core

By : Nigel George
Book Image

Mastering Django: Core

By: Nigel George

Overview of this book

Mastering Django: Core is a completely revised and updated version of the original Django Book, written by Adrian Holovaty and Jacob Kaplan-Moss - the creators of Django. The main goal of this book is to make you a Django expert. By reading this book, you’ll learn the skills needed to develop powerful websites quickly, with code that is clean and easy to maintain. This book is also a programmer’s manual that provides complete coverage of the current Long Term Support (LTS) version of Django. For developers creating applications for commercial and business critical deployments, Mastering Django: Core provides a complete, up-to-date resource for Django 1.8LTS with a stable code-base, security fixes and support out to 2018.
Table of Contents (33 chapters)
Mastering Django: Core
Credits
About the Author
www.PacktPub.com
Preface
Free Chapter
1
Introduction to Django and Getting Started

Making "friendly" template contexts


You might have noticed that our sample publisher list template stores all the publishers in a variable named object_list. While this works just fine, it isn't all that "friendly" to template authors: they have to "just know" that they're dealing with publishers here.

In Django, if you're dealing with a model object, this is already done for you. When you are dealing with an object or queryset, Django populates the context using the lower cased version of the model class' name. This is provided in addition to the default object_list entry, but contains exactly the same data, that is publisher_list.

If this still isn't a good match, you can manually set the name of the context variable. The context_object_name attribute on a generic view specifies the context variable to use:

# views.py 
from django.views.generic import ListView 
from books.models import Publisher 
 
class PublisherList(ListView): 
    model = Publisher 
    context_object_name...