Fix for: character of encoding “UTF8” has no equivalent in “LATIN1”, Ubuntu & Vagrant

This is mostly a post for if I happen over this problem again in the future.

Related to: “DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1.”

The solution I found is a bit of a hack. Really you want to find why postgres has created its databases in LATIN1 encoding before installing postgres.

This script however will recreate them correctly so you can get on with some work. Run it before creating your application database(s).

#!/usr/bin/env bash
# This script changes postgres from LATIN1 to UTF8
pg_dumpall > /tmp/postgres.sql
pg_dropcluster --stop 9.1 main
pg_createcluster --locale en_US.UTF-8 --start 9.1 main
psql -f /tmp/postgres.sql

Navigation active state in Django

Here’s a clean way to display a navigation menu item’s active state in Django.

Wherever the app you’re doing this for is located you’ll have an urls.py. Ensure you have the name set within each url group.

from django.conf.urls import patterns, url
from apps.pages import views

urlpatterns = patterns('',
    url(r'^pages/$', views.pages.index, name='pages.index'),
    url(r'^pages/about$', views.pages.about, name='pages.about'),
)

Next create a directory called templatetags within your app folder.
Add to it a blank __init__.py and nav_active.py, giving it the below content.

from django.core.urlresolvers import resolve
from django.template import Library

register = Library()

@register.simple_tag
def nav_active(request, url):
    """
    In template: {% nav_active request "url_name_here" %}
    """
    url_name = resolve(request.path).url_name
    if url_name == url:
        return "active"
    return ""

# nav_active() will check the web request url_name and compare it 
# to the named url group within urls.py, 
# setting the active class if they match.

Now to finish up, in your template .html file you need to load in the template tag and add it to each navigation item.

{% load nav_active %}

<nav>
    <a href="{% url 'myapp:pages.index' %}" class="{% nav_active request 'pages.index' %}">
        Home
    </a>
    <a href="{% url 'myapp:pages.about' %}" class="{% nav_active request 'pages.about' %}">
        About
    </a>
</nav>

How to use Enums for Django Field.choices

In Django when using the choices parameter on a form field the format passed in must be as follows:

# within your models.Model class...
STUDENT_TYPE_CHOICES = (
    ('0', 'freshman'),
    ('1', 'sophomore'),
    ('2', 'junior'),
    ('3', 'senior'),
)
student_type = models.CharField(max_length=1, choices=STUDENT_TYPE_CHOICES)

This means elsewhere in your code if you want to specify a choice field value, you’d have to enter the first slot of the tuple’s value, e.g.:

junior_students = Student.objects.filter(student_type='2')

This is pretty terrible since it’s hardcoded in our source, possibly over many files.

How to fix this mess:

In my project I added common/utils.py containing the following:

from enum import Enum

class ChoiceEnum(Enum):
    @classmethod
    def choices(cls):
        return tuple((i.name, i.value) for i in cls)

That’s the hard work over.

Now when you create your field with choices:

from common.utils import ChoiceEnum

class StudentTypes(ChoiceEnum):
    freshman = 0
    sophomore = 1
    junior = 2
    senior = 3

# within your models.Model class...
class Student(models.Model):
    student_type = models.CharField(max_length=1, choices=StudentTypes.choices())

 

Now if we need to access StudentTypes from elsewhere in our source code, we can simply:

junior_students = Student.objects.filter(student_type=StudentTypes.junior.value)

 

That’s it. If anyone knows of a nicer way feel free to comment below.

Setting up NGINX + Django + uWSGI (a tutorial that actually works)

So after reading the various tutorials online for setting up NGINX + Django + uWSGI and all of them not working correctly, I decided to write my own.

This tutorial was tested on a blank install of Ubuntu Server 12.04 LTS 64-bit, if you follow the steps carefully in the correct order all should be well :)

1. Add a new user, give them sudo privileges and switch to that user, below I’ve named mine “user”.

sudo adduser user
sudo adduser user sudo
su user

 

2. Ensure your system hostname is set to localhost

sudo echo "localhost" &gt; /etc/hostname
sudo hostname localhost

 

3. Since this is a new install, update the system.

sudo apt-get update
sudo apt-get upgrade

 

4. Install python, virtual environment builder and python dev

sudo apt-get install python
sudo apt-get install python-virtualenv
sudo apt-get install python2.7-dev

 

5. Install and start the NGINX web server

sudo apt-get install nginx
sudo service nginx start

 

6. Install uWSGI

sudo apt-get install uwsgi

 

7. Setup a Django project

sudo mkdir /var/www
sudo mkdir /var/www/example.com
cd /var/www/example.com
sudo mkdir venv conf src logs

 

This will give the below pictured folder structure
folder structure

 

8. Set-up the virtual environment and activate it

sudo virtualenv /var/www/example.com/venv
source /var/www/example.com/venv/bin/activate

 

9. Install Django

sudo pip install django

 

10. Change to the “src” directory, then copy your Django project files into it

cd /var/www/example.com/src

 

10. Create your uwsgi.ini config file, with the below content

sudo nano /var/www/example.com/conf/uwsgi.ini
[uwsgi]
# variables
projectname = example_project
projectdomain = example.com
base = /var/www/example.com

# config
plugins = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/src/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8889
logto = %(base)/logs/uwsgi.log
#below line runs it as a daemon in background
daemonize = /var/log/uwsgi/example_project.log

 

11. Create an NGINX config file for this domain, with the below content

sudo nano /var/www/example.com/conf/nginx.conf
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/src/example_project;
    access_log /var/www/example.com/logs/access.log;
    error_log /var/www/example.com/logs/error.log;

    location /static/ { # STATIC_URL
        alias /var/www/example.com/src/static/; # STATIC_ROOT
        expires 30d;
    }

    location /media/ { # MEDIA_URL
        alias /var/www/example.com/src/media/; # MEDIA_ROOT
        expires 30d;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8889;
    }
}

 

12. Edit the main nginx.conf to import our domain conf file, see below content as a guide

sudo nano /etc/nginx/nginx.conf
user    www-data;
# ...
http {
    # ...
    include /var/www/*/conf/nginx.conf;
    # ...
}

 

13. Restart NGINX (to load apply our config changes)

sudo service nginx restart

 

14. Install MySQL and secure it

sudo apt-get install mysql-server
sudo mysql_secure_installation

 

15. Install Python MySQL and uWSGI plugins

sudo apt-get install python-mysqldb
sudo apt-get install uwsgi-plugin-python

 

16. Install south (optional)

sudo pip install south

 

17. Test that uWSGI is working

sudo uwsgi --ini /var/www/example.com/conf/uwsgi.ini

If you visit your site it should now show django. If it doesn’t common causes are:

  • ALLOWED_HOSTS in settings.py isn’t set
  • DEBUG isn’t off in settings.py
  • Database isn’t configured

 

18. Setup uWSGI to run on system boot

Create the following file, with the below content

sudo nano /etc/init/uwsgi.conf
# Emperor uWSGI script

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]

exec uwsgi --master --die-on-term --emperor /var/www/example.com/conf/uwsgi.ini

 

19. Now reboot the server and navigate to your website

sudo reboot