Web Development with Django Cookbook

Over 70 practical recipes to create multilingual, responsive, and scalable websites with Django

Web Development with Django Cookbook

Cookbook
Aidas Bendoraitis

Over 70 practical recipes to create multilingual, responsive, and scalable websites with Django
$29.99
$49.99
RRP $29.99
RRP $49.99
eBook
Print + eBook
$12.99 p/month

Want this title & more? Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.
+ Collection
Free Sample

Book Details

ISBN 139781783286898
Paperback294 pages

About This Book

  • Improve your skills by developing models, forms, views, and templates
  • Create a rich user experience using Ajax and other JavaScript techniques
  • A practical guide to writing and using APIs to import or export data

Who This Book Is For

If you have created websites with Django, but you want to sharpen your knowledge and learn some good approaches for how to treat different aspects of web development, you should definitely read this book. It is intended for intermediate and professional Django users who need to build projects which must be multilingual, functional on devices of different screen sizes, and which scale over time.

Table of Contents

Chapter 1: Getting Started with Django 1.6
Introduction
Working with a virtual environment
Creating a project file structure
Handling project dependencies with pip
Including external dependencies in your project
Defining relative paths in the settings
Setting up STATIC_URL dynamically for Subversion users
Setting up STATIC_URL dynamically for Git users
Creating and including local settings
Setting UTF-8 as the default encoding for MySQL configuration
Setting the Subversion ignore property
Creating the Git ignore file
Deleting Python-compiled files
Importing an order in Python files
Defining overwritable app settings
Chapter 2: Database Structure
Introduction
Using model mixins
Creating a model mixin with URL-related methods
Creating a model mixin to handle creation and modification dates
Creating a model mixin to take care of meta tags
Creating a model mixin to handle generic relations
Handling multilingual fields
Using South migrations
Changing a foreign key to the many-to-many field with South
Chapter 3: Forms and Views
Introduction
Passing HttpRequest to the form
Utilizing the save method of the form
Uploading images
Creating a form layout with django-crispy-forms
Filtering object lists
Managing paginated lists
Composing class-based views
Generating PDF documents
Chapter 4: Templates and JavaScript
Introduction
Arranging the base.html template
Including JavaScript settings
Using HTML5 data attributes
Opening object details in a pop up
Implementing a continuous scroll
Implementing the Like widget
Uploading images by Ajax
Chapter 5: Custom Template Filters and Tags
Introduction
Following conventions for your own template filters and tags
Creating a template filter to show how many days have passed
Creating a template filter to extract the first media object
Creating a template filter to humanize URLs
Creating a template tag to include a template if it exists
Creating a template tag to load a QuerySet in a template
Creating a template tag to parse content as a template
Creating a template tag to modify request query parameters
Chapter 6: Model Administration
Introduction
Customizing columns in the change list page
Creating admin actions
Developing change list filters
Exchanging administration settings for external apps
Inserting a map into a change form
Chapter 7: Django CMS
Introduction
Creating templates for Django CMS
Structuring the page menu
Converting an app to a CMS app
Attaching your own navigation
Writing your own CMS plugin
Adding new fields to the CMS page
Chapter 8: Hierarchical Structures
Introduction
Creating hierarchical categories
Creating a category administration interface with django-mptt-admin
Creating a category administration interface with django-mptt-tree-editor
Rendering categories in a template
Using a single selection field to choose a category in forms
Using a checkbox list to choose multiple categories in forms
Chapter 9: Data Import and Export
Introduction
Importing data from a local CSV file
Importing data from a local Excel file
Importing data from an external JSON file
Importing data from an external XML file
Creating filterable RSS feeds
Using Tastypie to provide data to third parties
Chapter 10: Bells and Whistles
Introduction
Using the Django shell
The monkey patching slugification function
The monkey patching model administration
Toggling Debug Toolbar
Using ThreadLocalMiddleware
Caching the method value
Getting detailed error reporting via e-mail
Deploying on Apache with mod_wsgi
Creating and using the Fabric deployment script

What You Will Learn

  • Configure your Django project the right way
  • Build a database structure out of reusable model mixins
  • Manage hierarchical structures with MPTT
  • Play nicely with JavaScript in responsive templates
  • Create handy template filters and tags that you can reuse in every project
  • Master the configuration of contributed administration
  • Extend Django CMS with your own functionality

In Detail

Django is easy to learn and solves all types of web development problems and questions, providing Python developers an easy solution to web-application development. With a wealth of third-party modules available, you'll be able to create a highly customizable web application with this powerful framework.

Web Development with Django Cookbook will guide you through all web development processes with the Django framework. You will get started with the virtual environment and configuration of the project, and then you will learn how to define a database structure with reusable components. Find out how to tweak the administration to make the website editors happy. This book deals with some important third-party modules necessary for fully equipped web development.

 

 

Read an extract of the book

Creating Filterable RSS Feeds

Django comes with a syndication feed framework that allows you to create RSS and Atom feeds easily. RSS and Atom feeds are XML documents with specific semantics. They can be subscribed in an RSS reader such as Feedly, or they can be aggregated into other websites, mobile applications, or desktop applications. In this recipe, we will create BulletinFeed, which provides a bulletin board with images. Moreover, the results will be filterable by URL query parameters.

Getting ready

Create a new bulletin_board app and put it under INSTALLED_APPS in the settings.

How to do it…

We will create a Bulletin model and an RSS feed for it that can be filtered by type or category, so that the visitor can subscribe only to bulletins that are, for example, offering used books:

  1. In the models.py file of that app, add the models Category and Bulletin with a foreign key relationship between them:
    #bulletin_board/models.py
    # -*- coding: UTF-8 -*-
    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    from django.core.urlresolvers import reverse
    from utils.models import CreationModificationDateMixin
    from utils.models import UrlMixin
    
    TYPE_CHOICES = (
        ("searching", _("Searching")),
        ("offering", _("Offering")),
    )
    
    class Category(models.Model):
        title = models.CharField(_("Title"), max_length=200)
    
        def __unicode__(self):
            return self.title
    
        class Meta:
            verbose_name = _("Category")
            verbose_name_plural = _("Categories")
    
    
    class Bulletin(CreationModificationDateMixin, UrlMixin):
        bulletin_type = models.CharField(_("Type"), max_length=20, choices=TYPE_CHOICES)
        category = models.ForeignKey(Category, verbose_name=_("Category"))
        title = models.CharField(_("Title"), max_length=255)
        description = models.TextField(_("Description"), max_length=300)
        contact_person = models.CharField(_("Contact person"), max_length=255)
        phone = models.CharField(_("Phone"), max_length=200, blank=True)
        email = models.CharField(_("Email"), max_length=254, blank=True)
        image = models.ImageField(_("Image"), max_length=255, upload_to="bulletin_board/", blank=True)
    
        class Meta:
            verbose_name = _("Bulletin")
            verbose_name_plural = _("Bulletins")
            ordering = ("-created",)
    
        def __unicode__(self):
            return self.title
    
        def get_url_path(self):
            return reverse("bulletin_detail", kwargs={"pk": self.pk})
    
  2. Then, create BulletinFilterForm that allows the visitor to filter bulletins by type and by category, as follows:
    #bulletin_board/forms.py
    # -*- coding: UTF-8 -*-
    from django import forms
    from django.utils.translation import ugettext_lazy as _
    from models import Category, TYPE_CHOICES
    
    class BulletinFilterForm(forms.Form):
        bulletin_type = forms.ChoiceField(
            label=_("Bulletin Type"),
            required=False,
            choices=(("", "---------"),) + TYPE_CHOICES,
        )
        category = forms.ModelChoiceField(
            label=_("Category"),
            required=False,
            queryset=Category.objects.all(),
        )
    
  3. Add a feeds.py file with the BulletinFeed class inside, as follows:
    #bulletin_board/feeds.py
    # -*- coding: UTF-8 -*-
    from django.contrib.syndication.views import Feed
    from django.core.urlresolvers import reverse
    
    from models import Bulletin, TYPE_CHOICES
    from forms import BulletinFilterForm
    
    class BulletinFeed(Feed):
        description_template = "bulletin_board/feeds/bulletin_description.html"
    
        def get_object(self, request, *args, **kwargs):
            form = BulletinFilterForm(data=request.REQUEST)
            obj = {}
            if form.is_valid():
                obj = {
                    "bulletin_type": form.cleaned_data["bulletin_type"],
                    "category": form.cleaned_data["category"],
                    "query_string": request.META["QUERY_STRING"],
                }
            return obj
    
        def title(self, obj):
            t = u"My Website - Bulletin Board"
            # add type "Searching" or "Offering"
            if obj.get("bulletin_type", False):
                tp = obj["bulletin_type"]
                t += u" - %s" % dict(TYPE_CHOICES)[tp]
            # add category
            if obj.get("category", False):
                t += u" - %s" % obj["category"].title
            return t
    
        def link(self, obj):
            if obj.get("query_string", False):
                return reverse("bulletin_list") + "?" + obj["query_string"]
            return reverse("bulletin_list")
    
        def feed_url(self, obj):
            if obj.get("query_string", False):
                return reverse("bulletin_rss") + "?" + obj["query_string"]
            return reverse("bulletin_rss")
    
        def item_pubdate(self, item):
            return item.created
    
        def items(self, obj):
            qs = Bulletin.objects.order_by("-created")
            if obj.get("bulletin_type", False):
                qs = qs.filter(
                    bulletin_type=obj["bulletin_type"],
                ).distinct()
            if obj.get("category", False):
                qs = qs.filter(
                    category=obj["category"],
                ).distinct()
            return qs[:30]
    
  4. Create a template for the bulletin description in the feed as follows:
    {#templates/bulletin_board/feeds/bulletin_description.html#}
    {% if obj.image %}
        <p><a href="{{ obj.get_url }}"><img src="http://{{ request.META.HTTP_HOST }}{{ obj.image.url }}" alt="" /></a></p>
    {% endif %}
    <p>{{ obj.description }}</p>
    
  5. Create a URL configuration for the bulletin board app and include it in the root URL configuration, as follows:
    #templates/bulletin_board/urls.py
    # -*- coding: UTF-8 -*-
    from django.conf.urls import *
    from feeds import BulletinFeed
    
    urlpatterns = patterns("bulletin_board.views",
        url(r"^$", "bulletin_list", name="bulletin_list"),
        url(r"^(?P<bulletin_id>[0-9]+)/$", "bulletin_detail", name="bulletin_detail"),
        url(r"^rss/$", BulletinFeed(), name="bulletin_rss"),
    )
    
  6. You will also need the views and templates for the filterable list and details of the bulletins. In the Bulletin list page template, add this link:
    <a href="{% url "bulletin_rss" %}?{{ request.META.QUERY_STRING }}">RSS Feed</a>

How it works…

So, if you have some data in the database and you open http://127.0.0.1:8000/bulletin-board/rss/?bulletin_type=offering&category=4 in your browser, you will get an RSS feed of bulletins with the type Offering and category ID 4.

The BulletinFeed class has the get_objects method that takes the current HttpRequest and defines the obj dictionary used in other methods of the same class.

The obj dictionary contains the bulletin type, category, and current query string.

The title method returns the title of the feed. It can either be generic or related to the selected bulletin type or category. The link method returns the link to the original bulletin list with the filtering done. The feed_url method returns the URL of the current feed. The items method does the filtering itself and returns a filtered QuerySet of bulletins. And finally, the item_pubdate method returns the creation date of the bulletin.

To see all the available methods and properties of the Feed class that we are extending, refer to the following documentation: https://docs.djangoproject.com/en/1.6/ref/contrib/syndication/#feed-class-reference

The other parts of the code are kind of self-explanatory!

Authors

Table of Contents

Chapter 1: Getting Started with Django 1.6
Introduction
Working with a virtual environment
Creating a project file structure
Handling project dependencies with pip
Including external dependencies in your project
Defining relative paths in the settings
Setting up STATIC_URL dynamically for Subversion users
Setting up STATIC_URL dynamically for Git users
Creating and including local settings
Setting UTF-8 as the default encoding for MySQL configuration
Setting the Subversion ignore property
Creating the Git ignore file
Deleting Python-compiled files
Importing an order in Python files
Defining overwritable app settings
Chapter 2: Database Structure
Introduction
Using model mixins
Creating a model mixin with URL-related methods
Creating a model mixin to handle creation and modification dates
Creating a model mixin to take care of meta tags
Creating a model mixin to handle generic relations
Handling multilingual fields
Using South migrations
Changing a foreign key to the many-to-many field with South
Chapter 3: Forms and Views
Introduction
Passing HttpRequest to the form
Utilizing the save method of the form
Uploading images
Creating a form layout with django-crispy-forms
Filtering object lists
Managing paginated lists
Composing class-based views
Generating PDF documents
Chapter 4: Templates and JavaScript
Introduction
Arranging the base.html template
Including JavaScript settings
Using HTML5 data attributes
Opening object details in a pop up
Implementing a continuous scroll
Implementing the Like widget
Uploading images by Ajax
Chapter 5: Custom Template Filters and Tags
Introduction
Following conventions for your own template filters and tags
Creating a template filter to show how many days have passed
Creating a template filter to extract the first media object
Creating a template filter to humanize URLs
Creating a template tag to include a template if it exists
Creating a template tag to load a QuerySet in a template
Creating a template tag to parse content as a template
Creating a template tag to modify request query parameters
Chapter 6: Model Administration
Introduction
Customizing columns in the change list page
Creating admin actions
Developing change list filters
Exchanging administration settings for external apps
Inserting a map into a change form
Chapter 7: Django CMS
Introduction
Creating templates for Django CMS
Structuring the page menu
Converting an app to a CMS app
Attaching your own navigation
Writing your own CMS plugin
Adding new fields to the CMS page
Chapter 8: Hierarchical Structures
Introduction
Creating hierarchical categories
Creating a category administration interface with django-mptt-admin
Creating a category administration interface with django-mptt-tree-editor
Rendering categories in a template
Using a single selection field to choose a category in forms
Using a checkbox list to choose multiple categories in forms
Chapter 9: Data Import and Export
Introduction
Importing data from a local CSV file
Importing data from a local Excel file
Importing data from an external JSON file
Importing data from an external XML file
Creating filterable RSS feeds
Using Tastypie to provide data to third parties
Chapter 10: Bells and Whistles
Introduction
Using the Django shell
The monkey patching slugification function
The monkey patching model administration
Toggling Debug Toolbar
Using ThreadLocalMiddleware
Caching the method value
Getting detailed error reporting via e-mail
Deploying on Apache with mod_wsgi
Creating and using the Fabric deployment script

Book Details

ISBN 139781783286898
Paperback294 pages
Read More

Recommended for You

Django Essentials
$ 13.99
Instant Django 1.5 Application Development Starter
$ 12.99
Django JavaScript Integration: AJAX and jQuery
$ 26.99