Missing code to include staticfiles

If you are reading Writing your first Django app, part 6, the code won’t work until you put in the following code from https://docs.djangoproject.com/en/1.8/howto/static-files/ (in the section named Serving static files during development) to the urls.py file.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Basically your urls.py needs to know where to route the static file requests. =)

Django using TIME_ZONE to display local time

It took me a while to figure out how to do this correctly. There is too much noise from google search… Here are my settings for everything to display correctly in my admin site w/ Django 1.8.


USE_TZ = True
TIME_ZONE = 'US/Pacific'
HUMAN_TIME_FMT = '%Y-%m-%d %H:%M:%S %p'

HUMAN_TIME_FMT is optional, I put it there for my own usage.


from django.conf import settings
from django.db import models
from django.utils import timezone

class Sync(models.Model):

    sync_start = models.DateTimeField('sync starts')
    sync_end = models.DateTimeField('sync ends')
    def __str__(self):
        fmt = settings.HUMAN_TIME_FMT
        my_dt1 = timezone.localtime(self.sync_start).strftime(fmt)
        my_dt2 = timezone.localtime(self.sync_end).strftime(fmt)
        return 'From ' + my_dt1 + ' => ' + my_dt2

More to read:
# https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-TIME_ZONE
# http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# http://strftime.org/

Excel to JSON

Here is my little side project to convert excel files to json objects. I mainly used the Google App Engine and the Python xlrd module.

Feel free to try it out at http://excel2json.appspot.com/.

curl -F f=@my_file.xls http://excel2json.appspot.com/2json

All you need is just one line of code!

Working with Excel files in Python

The libraries needed are: xlutils, xlrd and xlwt. They are all under the package called xlutils. So just issue the following command.

sudo pip install xlutils

To verify:

localhost:~ birdchan$ python
Python 2.7.2 (default, Jun 16 2012, 12:38:40) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlutils

Should have no error messages.

mysql python installation error on osx

I installed MySQL-Python (version 1.2.3 at the time being) from http://sourceforge.net/projects/mysql-python/ on my osx 10.6, python version 2.6.1. Just do the usual “python setup.py build” and “python setup.py install”. However though, in the python cli, when I do “import MySQLdb”, I got some error.

>>> import MySQLdb
Traceback (most recent call last):
  File "", line 1, in 
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in 
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in 
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/birdchan/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/birdchan/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Reason: image not found

Obviously, the error is “Library not loaded: libmysqlclient.18.dylib”. A quick fix is to simply create a symlink.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

I suppose you can also play with your env path.

Regular Expression in Python

Just found this handy online tool to try out regular expressions. It helps when you want to focus on the regular expression more than your python code.