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

List/detail generic views


The list/detail generic views handle the common case of displaying a list of items at one view and individual detail views of those items at another.

Lists of objects

django.views.generic.list.ListView 

Use this view to display a page representing a list of objects.

Example views.py:

from django.views.generic.list import ListView 
from django.utils import timezone 
 
from articles.models import Article 
 
class ArticleListView(ListView): 
 
    model = Article 
 
    def get_context_data(self, **kwargs): 
        context = super(ArticleListView, self).get_context_data(**kwargs) 
        context['now'] = timezone.now() 
        return context 

Example myapp/urls.py:

from django.conf.urls import url 
 
from article.views import ArticleListView 
 
urlpatterns = [ 
    url(r'^$', ArticleListView.as_view(), name='article-list'), 
] 

Example myapp/article_list.html:

<h1>Articles</h1> 
<ul> 
{% for article in object_list %} 
    <li>{{ article.pub_date|date }}-{{ article.headline }}</li> 
{% empty %} 
    <li>No articles yet.</li> 
{% endfor %} 
</ul> 

Detail views

django.views.generic.detail.DetailView

This view provides a detail view of a single object.

Example myapp/views.py:

from django.views.generic.detail import DetailView 
from django.utils import timezone 
 
from articles.models import Article 
 
class ArticleDetailView(DetailView): 
 
    model = Article 
 
    def get_context_data(self, **kwargs): 
        context = super(ArticleDetailView,  
                        self).get_context_data(**kwargs) 
        context['now'] = timezone.now() 
        return context 

Example myapp/urls.py:

from django.conf.urls import url 
 
from article.views import ArticleDetailView 
 
urlpatterns = [ 
    url(r'^(?P<slug>[-_\w]+)/$',  
        ArticleDetailView.as_view(),  
        name='article-detail'), 
] 

Example myapp/article_detail.html:

<h1>{{ object.headline }}</h1> 
<p>{{ object.content }}</p> 
<p>Reporter: {{ object.reporter }}</p> 
<p>Published: {{ object.pub_date|date }}</p> 
<p>Date: {{ now|date }}</p>