• 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



.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Adtech Affiliate Marketing Afilias Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use DeviceAtlas Display dmexco DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP HTTP Headers Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia m.dot macOS Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Nokia 3310 Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV programmatic PS4 Publishers Python QMobile QR Codes Redirection Research Reseller Responsive Images Responsive web design RESS Review reviews RIM Ringmark RTB RWD Safari Samsung Scala ScientiaMobile Screen Screen resolution Screen Size SEO Server Server-side optimisation Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore Sitecore version 9 SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW Tutorial TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 Video VoLTE VR Wearable Web Web Apps Web content management WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone WURFL Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE