• About Us
  • Blog
  • Basket
  • Account
  • Sign In


Published on Friday, June 28, 2013

Using 51Degrees.mobi's new Python solution with Django


Due to the launch of the new 51Degrees Python device detection solution I have written this tutorial to demonstrate its functionality and how you can implement it in your python environment. Below is a short tutorial taking you through the setting up of the 51Degrees.mobi Python solution using an example Django website.

What set up will I be using?

In this example I used a fresh m1.large instance running a 64-bit version of Ubuntu 12.04 on Amazon Web Services.

Once I set the instance up, I ensured that all the packages were up to date by running the following command in the remote machine terminal. This will help avoid using any out-dated packages.

$ sudo apt-get update

There are several other packages/tools that are needed so that you will be able to use 51Degrees.mobi Python packages. They are: python, GCC, make, pip, python-dev and django. GCC and Make are required to build the package as native C code is distributed for faster detection. You can install all of these using the following commands:

$ sudo apt-get install python (this is most likely already installed)
$ sudo apt-get install pip
$ sudo apt-get install gcc
$ sudo apt-get install python-dev
$ sudo apt-get install make
$ sudo pip install django

Once everything is installed, follow the steps below.

Step 1: Download the Django demo zip folder

Download the django-demo.tar.gz

Step 2: Copy the Django demo folder to the remote machine

Copy the django-demo zip file that you download to your computer and copy it to your remote machine. This is done by navigating to the django-demo zip file and entering the following command (in your computer's terminal, not the remote machine's):

$ scp -i <KeyPairName> django-demo.tar.gz <name of the remote machine>:

The “:” at the end of the command means “HOME”. If you want to put the file in a specific place on the remote machine, replace the “:” with your desired location.

If the file copied correctly, a message similar to this will display in the terminal.

django-demo.tar.gz                     100% 3612     3.5KB/s   00:00

To check that the file is actually on your remote computer, go to your remote computer terminal, navigate to the HOME directory (or wherever you copied it too) and use the ls command. For example:

$ cd ~
$ ls 

You should now see django-demo.tar.gz being listed.

Step 3: Extract Django-demo

You will now need to extract the django-demo.tar.gz using the following command:

$ tar -zxvf django-demo.tar.gz

Run $ ls again and you should now see:

django-demo      django-demo-tar.gz

Step 4: Install 51Degrees.mobi matching method

Install the 51Degrees matching method you would like. In this example we will be using the lite-pattern-wrapper.

You can download your desired 51Degrees.mobi Python package, Lite or Premium, manually or Lite can be downloaded using the "pip" command. This may take a couple of minutes.

$ sudo pip install 51degrees-mobile-detector-lite-pattern-wrapper

Step 5: Running Django

Navigate to the django-demo folder and run the following command:

$ sudo python manage.py runserver

Ensure that port 80 is open in your instance's security group.

Paste the public address of your remote machine into your browser and you should get something display like below in your browser (obviously it will look different depending on your settings)

Step 6: Changing your settings

Navigate to the folder which contains Django settings file, settings.py

$ cd django-demo/demo

Display the files in the directory using $ ls

__init__.py   settings.py   templates  urls.pyc  views.pyc  wsgi.pyc
__init__.pyc  settings.pyc  urls.py    views.py  wsgi.py

To open settings.py use the following command

$ sudo nano settings.py

This will display the file's content in the command window. The section that we are interested in is:

    'DETECTION_METHOD': 'lite-pattern-wrapper',
    'PROPERTIES': ('Id', 'IsMobile', 'WebWorkers', 'Html5',),

Here you can change the detection method, the properties you want and also change usage sharing with 51Degrees.mobi. Below, I have changed the settings file to use the Premium pattern wrapper, display the properties: IsMobile, IsTablet and ScreenPixelsWidth, and also switch off usage sharing.

    'DETECTION_METHOD': 'premium-pattern-wrapper',
    'PROPERTIES': ('IsMobile', 'IsTablet', 'ScreenPixelsWidth',),

Once you have completed these steps you should be able to understand what devices are browsing your website. You will then be able to create 'rules' and offer optimised web pages to specific devices or device capabilities.

If you need to offer tablets, smartphones or eReaders different content or interfaces then you will need to upgrade to 51Degrees.mobi Premium Device Data. For more information on how to install the Premium version of 51Degrees.mobi, follow our documentation.

For more, see our documentation and our packages on PyPI and GitHub.

Comments (0)

Author: Data Team

Categories: Development

Tags: Python

Piran Watson

Data Team

Other posts by Data Team
Contact author