Book Image

Web Development with Django Cookbook- Second Edition - Second Edition

By : Aidas Bendoraitis
Book Image

Web Development with Django Cookbook- Second Edition - Second Edition

By: Aidas Bendoraitis

Overview of this book

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.
Table of Contents (18 chapters)
Web Development with Django Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making your code compatible with both Python 2.7 and Python 3


Since version 1.7, Django can be used with Python 2.7 and Python 3. In this recipe, we will take a look at the operations to make your code compatible with both the Python versions.

Getting ready

When creating a new Django project or upgrading an old existing project, consider following the rules given in this recipe.

How to do it…

Making your code compatible with both Python versions consists of the following steps:

  1. At the top of each module, add from __future__ import unicode_literals and then use usual quotes without a u prefix for Unicode strings and a b prefix for bytestrings.

  2. To ensure that a value is bytestring, use the django.utils.encoding.smart_bytes function. To ensure that a value is Unicode, use the django.utils.encoding.smart_text or django.utils.encoding.force_text function.

  3. For your models, instead of the __unicode__ method, use the __str__ method and add the python_2_unicode_compatible decorator, as follows:

    # models.py
    # -*- coding: UTF-8 -*-
    from __future__ import unicode_literals
    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    from django.utils.encoding import \
        python_2_unicode_compatible
    
    @python_2_unicode_compatible
    class NewsArticle(models.Model):
        title = models.CharField(_("Title"), max_length=200)
        content = models.TextField(_("Content"))
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = _("News Article")
            verbose_name_plural = _("News Articles")
  4. To iterate through dictionaries, use iteritems(), iterkeys(), and itervalues() from django.utils.six. Take a look at the following:

    from django.utils.six import iteritems
    d = {"imported": 25, "skipped": 12, "deleted": 3}
    for k, v in iteritems(d):
        print("{0}: {1}".format(k, v))
  5. When you capture exceptions, use the as keyword, as follows:

    try:
        article = NewsArticle.objects.get(slug="hello-world")
    except NewsArticle.DoesNotExist as exc:
        pass
    except NewsArticle.MultipleObjectsReturned as exc:
        pass
  6. To check the type of a value, use django.utils.six, as shown in the following:

    from django.utils import six
    isinstance(val, six.string_types) # previously basestring
    isinstance(val, six.text_type) # previously unicode
    isinstance(val, bytes) # previously str
    isinstance(val, six.integer_types) # previously (int, long)
  7. Instead of xrange, use range from django.utils.six.moves, as follows:

    from django.utils.six.moves import range
    for i in range(1, 11):
        print(i)
  8. To check whether the current version is Python 2 or Python 3, you can use the following conditions:

    from django.utils import six
    if six.PY2:
        print("This is Python 2")
    if six.PY3:
        print("This is Python 3")

How it works…

All strings in Django projects should be considered as Unicode strings. Only the input of HttpRequest and output of HttpResponse is usually in the UTF-8 encoded bytestring.

Many functions and methods in Python 3 now return the iterators instead of lists, which make the language more efficient. To make the code compatible with both the Python versions, you can use the six library that is bundled in Django.

Read more about writing compatible code in the official Django documentation at https://docs.djangoproject.com/en/1.8/topics/python3/.

Tip

Downloading the example code

You can download the example code files for all Packt books that 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 in order to have the files e-mailed directly to you.