You are previewing Web Development with Django Cookbook - Second Edition.
O'Reilly logo
Web Development with Django Cookbook - Second Edition

Book Description

Over 70 practical recipes to help you create scalable websites using the Django 1.8 framework

About This Book

  • This is the latest book on the market that will help you take advantage of the new features added to Django 1.8

  • This book consists of recipes of varying complexities to help you create multilingual, responsive, and scalable websites with Django

  • This updated edition teaches you major Django functions and will help you improve your skills by developing models, forms, views, and templates

  • Who This Book Is For

    This book is for intermediate-level and professional Django users who need to build projects that are multilingual, functional on devices of different screen sizes, and that scale over a period of time. If you have created websites with Django but you want to sharpen your knowledge and learn some good approaches to different aspects of web development, you should definitely read this book.

    What You Will Learn

  • Get started with the basic configuration necessary to start any Django project

  • Build a database structure out of reusable model mixins

  • Manage forms and views and get to know some useful patterns that are used to create them

  • Create handy template filters and tags that you can reuse in every project

  • Integrate your own functionality into the Django CMS

  • Manage hierarchical structures with MPTT

  • Import data from local sources and external web services as well as exporting your data to third parties

  • Implement a multilingual search with Haystack

  • Test and deploy your project efficiently

  • In Detail

    Django is a web framework that was designed to strike a balance between rapid web development and high performance. It has the capacity to handle applications with high levels of user traffic and interaction, and can integrate with massive databases on the backend, constantly collecting and processing data in real time.

    Through this book, you'll discover that collecting data from different sources and providing it to others in different formats isn't as difficult as you thought. It follows a task-based approach to guide you through all the web development processes using the Django framework. We’ll start by setting up the virtual environment for a Django project and configuring it. Then you’ll learn to write reusable pieces of code for your models and find out how to manage database schema changes using South migrations. After that, we’ll take you through working with forms and views to enter and list data. With practical examples on using templates and JavaScript together, you will discover how to create the best user experience. In the final chapters, you'll be introduced to some programming and debugging tricks and finally, you will be shown how to test and deploy the project to a remote dedicated server.

    By the end of this book, you will have a good understanding of the new features added to Django 1.8 and be an expert at web development processes.

    Style and approach

    Every chapter consists of practical examples and a mix of basic and advanced recipes that will guide you through the entire web development process, starting from project configuration and taking you right through to deployment.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Web Development with Django Cookbook Second Edition
      1. Table of Contents
      2. Web Development with Django Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with Django 1.8
        1. Introduction
        2. Working with a virtual environment
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Creating a project file structure
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Handling project dependencies with pip
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Making your code compatible with both Python 2.7 and Python 3
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Including external dependencies in your project
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Configuring settings for development, testing, staging, and production environments
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Defining relative paths in the settings
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        9. Creating and including local settings
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Setting up STATIC_URL dynamically for Subversion users
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Setting up STATIC_URL dynamically for Git users
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        12. Setting UTF-8 as the default encoding for MySQL configuration
          1. Getting ready
          2. How to do it…
          3. How it works…
        13. Setting the Subversion ignore property
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        14. Creating the Git ignore file
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        15. Deleting Python-compiled files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        16. Respecting the import order in Python files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        17. Creating app configuration
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There is more…
          5. See also
        18. Defining overwritable app settings
          1. Getting ready
          2. How to do it…
          3. How it works…
      9. 2. Database Structure
        1. Introduction
        2. Using model mixins
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Creating a model mixin with URL-related methods
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Creating a model mixin to handle creation and modification dates
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Creating a model mixin to take care of meta tags
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Creating a model mixin to handle generic relations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Handling multilingual fields
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Using migrations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        9. Switching from South migrations to Django migrations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Changing a foreign key to the many-to-many field
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      10. 3. Forms and Views
        1. Introduction
        2. Passing HttpRequest to the form
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Utilizing the save method of the form
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Uploading images
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more
          5. See also
        5. Creating a form layout with django-crispy-forms
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Downloading authorized files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Filtering object lists
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Managing paginated lists
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        9. Composing class-based views
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        10. Generating PDF documents
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Implementing a multilingual search with Haystack
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      11. 4. Templates and JavaScript
        1. Introduction
        2. Arranging the base.html template
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Including JavaScript settings
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Using HTML5 data attributes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Opening object details in a modal dialog
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Implementing a continuous scroll
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Implementing the Like widget
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Uploading images by Ajax
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      12. 5. Custom Template Filters and Tags
        1. Introduction
        2. Following conventions for your own template filters and tags
          1. How to do it...
        3. Creating a template filter to show how many days have passed since a post was published
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Creating a template filter to extract the first media object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating a template filter to humanize URLs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Creating a template tag to include a template if it exists
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Creating a template tag to load a QuerySet in a template
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Creating a template tag to parse content as a template
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Creating a template tag to modify request query parameters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      13. 6. Model Administration
        1. Introduction
        2. Customizing columns on the change list page
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Creating admin actions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Developing change list filters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Customizing default admin settings
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Inserting a map into a change form
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      14. 7. Django CMS
        1. Introduction
        2. Creating templates for Django CMS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Structuring the page menu
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Converting an app to a CMS app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Attaching your own navigation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Writing your own CMS plugin
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Adding new fields to the CMS page
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      15. 8. Hierarchical Structures
        1. Introduction
        2. Creating hierarchical categories
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Creating a category administration interface with django-mptt-admin
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Creating a category administration interface with django-mptt-tree-editor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Rendering categories in a template
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Using a single selection field to choose a category in forms
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Using a checkbox list to choose multiple categories in forms
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      16. 9. Data Import and Export
        1. Introduction
        2. Importing data from a local CSV file
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Importing data from a local Excel file
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Importing data from an external JSON file
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Importing data from an external XML file
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Creating filterable RSS feeds
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Using Tastypie to create API
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Using Django REST framework to create API
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      17. 10. Bells and Whistles
        1. Introduction
        2. Using the Django shell
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Using database query expressions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Monkey-patching the slugify() function for better internationalization support
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Toggling the Debug Toolbar
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Using ThreadLocalMiddleware
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Caching the method return value
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Using Memcached to cache Django views
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Using signals to notify administrators about new entries
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        10. Checking for missing settings
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      18. 11. Testing and Deployment
        1. Introduction
        2. Testing pages with Selenium
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Testing views with mock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Testing API created using Django REST framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Releasing a reusable Django app
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Getting detailed error reporting via e-mail
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Deploying on Apache with mod_wsgi
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Setting up cron jobs for regular tasks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Creating and using the Fabric deployment script
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      19. Index