Solutions Log

So I only have to figure things out once.

Starting a Magento Theme

Duplicate these folders:

app/design/frontend/default/default
skin/frontend/default/default

(Don’t get me started about how awkward the folder structure is.)

Rename the duplicated folders to whatever you want (make them both the same).

FYI, the theme files under the app/ folder are for generated templates (things the system has to build when the site is viewed) and the theme files under the skin/ folder are for publicly viewable files (images, CSS, JavaScript).

Now go to your the admin area of your site and then System > Configuration. Then click Design in the sidebar and under Themes type the name of your theme folders (the ones you duplicated). Just once. One name.

Now the fun really begins. Time to dig into a bluemillion .phtml and .xml files. I’m still not too far along here, but I have determined that to change the media that you load in most pages, you need to edit:

app/design/frontend/default/your_theme_name/layout/page.xml

There you’ll se a bunch of crap like this:

<action method="addCss"><stylesheet>css/reset.css</stylesheet></action>

Have fun.

Using Someone Else’s SVN Repository With Git

If you have a repository URL that looks like this:

http://code.yourmom.com/project/trunk/

Issue this command (note that you leave off trunk/):

git svn clone -s http://code.yourmom.com/project/ project

After it’s done, see how big it is:

du -hs project

And you’ll see something like this:

20M project/

If it’s particularly big, go into the folder and garbage collect:

cd project
git gc

From within the project folder, set your local repository to the trunk (it’s set to whatever branch had the last commit otherwise):

git reset --hard trunk

Create your own branch and get to work:

git co -b treys_changes

When you want to pull in the changes from the original author to stay up to date:

git svn rebase

If you’ve cloned this repo (after posting it to GitHub or elsewhere) and want to use it on another computer, you’ll have to use do more step in order to track the original SVN repo again:

git update-ref refs/remotes/trunk origin/master
git svn init -T trunk http://code.yourmom.com/project/

Sources

Use Django’s Permalink Decorator With Generic Views

The permalink decorator is the way to keep your URLs DRY. The only place you need to define where something lives is in your urls.py. In your templates, just point to `. The problem with how they tell you to do it in [the book](http://djangobook.com/en/1.0/appendixB/#cn280), [the documentation](http://www.djangoproject.com/documentation/model-api/#the-permalink-decorator), and [elsewhere](http://cammacrae.com/blog/2007/08/20/permalink-decorator-pt-2/) is that it doesn't work right if you're using the same view function more than once in all yoururls.py` files (which is bound to happen if you’re using a bunch of generic views). That’s because Django has no way of telling which one you mean.

Named URL patterns to the rescue.

If you have a URLpattern that looks like this:

(r'^(?P<slug>[-\w]+)/$', list_detail.object_detail, log_detail),

Change it to this:

url(r'^(?P<slug>[-\w]+)/$', list_detail.object_detail, log_detail, name='log-detail'),

Note the addition of ”url” to the start of the line and the ”name=” bit at the end.

Now, in your model file(s), do the following:

Add this line to the top of the file:

from django.db.models import permalink

At the bottom of the model class do something similar to this:

@permalink
def get_absolute_url(self):
    return ('log-detail', (), { 'slug': self.slug })

Any other question you have about this stuff is probably in the other sources.

Sources:

Python Datatypes

dictionary = {'a':'apple', 'b':'banana', 'c':'cat'}
    # Associative array / hash / array with non-numeric indices.
list = ['a', 'b', 'c']
    # Normal array with 0-based indices.
tuple = ('a', 'b', 'c')
    # Read-only array.

More…

Django Template System Basics

The long way

  1. Load a template
  2. Fill a Context
  3. Return a HttpResponse object

Like so:

from django.template import Template, Context
from django.http import HttpResponse
...
t = get_template('current_datetime.html')
html = t.render(Context({'current_date': now}))
return HttpResponse(html)

The short way: render_to_response

Like so:

from django.shortcuts import render_to_response
...
return render_to_response('current_datetime.html', {'current_date': now})

Source:

Things You Probably Want to Install to Get the Most Out of Django

libjpeg (for PIL)

curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure
make
sudo make install-lib

Python Image Library (PIL)

curl -O http://effbot.org/media/downloads/Imaging-1.1.6.tar.gz
tar zxvf Imaging-1.1.6.tar.gz 
cd Imaging-1.1.6
sudo python setup.py install

Python Markdown library

curl -O http://pypi.python.org/packages/source/M/Markdown/markdown-1.7.tar.gz
tar zxvf markdown-1.7.tar.gz
cd markdown-1.7
sudo python setup.py install

See also.

Docutils

This makes Django’s built-in admin documentation work.

curl -O http://docutils.sourceforge.net/docutils-snapshot.tgz
tar zxvf docutils-snapshot.tgz
cd docutils
sudo python setup.py install

Sources

Installing Django on OS X Leopard

If you haven’t already, stick your Leopard disk in and install Xcode 3.0.

Make a home for Django:

mkdir -p ~/src/django/core
cd !$

Get the Django trunk from Subversion:

svn co http://code.djangoproject.com/svn/django/trunk/

The Django trunk should now be in ~/src/django/core/trunk/, and if you ever want to check out another branch, you have a nice spot to put it next to the trunk.


If you’ve previously install Python yourself, pay attention here

Find out if you’re using the right version of Python:

which python

If you don’t see:

/usr/bin/python

then delete whatever version you have sitting in your path, such as one in /usr/local/:

sudo rm /usr/local/bin/python

Once which python gives you /usr/bin/python, make sure your Python site-packages is in the right place. Running this command:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

should give you:

/Library/Python/2.5/site-packages

End of previous Python caveat


Now make sure Python knows where to find Django:

ln -s ~/src/django/core/trunk/django /Library/Python/2.5/site-packages/django

Put the django-admin.py script on your system path:

sudo ln -s ~/src/django/core/trunk/django/bin/django-admin.py /usr/local/bin/

Now you should have Django installed and ready to go. Run:

django-admin.py startproject project_name

and see.

If it works, cd into the folder it creates and try:

python manage.py runserver

Then go to localhost:8000 in your browser.

Django + MySQL:

If you want to use MySQL with Django, there’s a bit more to do. If you have MySQL ready to go, continue. Otherwise, go talk with Dan for a bit and come back here when you’re done.

Download the MySQLdb package. Stick in in /usr/local/src if you’re cool. Open it and edit site.cfg.

Change line ~ 13 from:

#mysql_config = /usr/local/bin/mysql_config

To:

mysql_config = /usr/local/mysql/bin/mysql_config

In _mysql.c:

Remove these lines (~ 37-39):

#ifndef uint 
#define uint unsigned int 
#endif

Go to the folder on the command line, then:

sudo python setup.py build
sudo python setup.py install

You might have to run the build command more than once. Don’t ask me why–I’m a copy and paster just like you.

That should do it. Try editing your settings.py file in your Django project and entering information for a MySQL database and see if it works.

If you experience something different than this or have any problems, please let me know directly or leave a comment.

Sources