Django

Installation on Windows XP - 14/2/2009

Install Python 2.6 for Windows. Install the subverion windows client.
$ cd C:\Python26\Lib\site-packages $ svn co hcodep://code.djangoproject.com/svn/django/trunk/ django-trunk Set environment variables. Right click on my computer > Advanced > Environment Variables
Add C:\python26 to the PATH environment variable
Create a new variable PYTHONPATH and set the variable value to C:\Python26\Lib\site-packages\django-trunk

Starting and Stopping Local Server

$python manage.py runserver

Working with the Django API on the command line

$ python manage.py shell >>> from mysite.polls.models import Poll, Choice

Resecodeing the Django development server database password

This method will create a new empty database
Rename the database file $ mv sqlite3.db sqlite3.db.old $python manage.pl syncdb

Image Magick - Making Thumbnails

Converts JPGs to thumbnails.
$ mogrify -resize 150x150 -background white -gravity center -extent 150x150 -format .jpg -quality 75 -path thumbs .jpg

Sqlite

Show tables .tables
Show header .header
Show schema .schema
Exit Sqlite .exit
Set the separator before doing an import, in this case a tab delimited file. .separator "\t" Always save import files as UTF-8 before doing an import

Excel - Series Fill

Open Excel and enter 1 into first cell. Then go to Edit > Fill > Series. Select "column", step Value=1, stop value = < user defined>
Note: Saving the file as "unicode text (*.txt)", unsure of exact encoding maybe UTF-16.

Running the development server on a local network

Allows other computers to see developement server
$ django-admin runserver [port or ip_address:port]

Import OS path

TEMPLATE DIRS = (os.path.join(os.path.dirname(__file__,'templates'),)

Selecting related fields in a Django ORM query

gal__ is the name of relationship in the Artist model that relates to a Gallery model. Slug is the name of the field we want from the model.
>>> q = Artist.objects.select_related('gal__slug').filter(last__istartswith='a').order_by('last','first') This query will return Artists object and inside this object will be a "gallery" object containing the slug field we want.

Serving static content from development server

Secodeings File
MEDIA_ROOT = r'c:/projects/art2/static/' MEDIA_URL = '/static/' ADMIN_MEDIA_PREFIX = '/media/'

Root URL Conf File
url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': r'c:/projects/art2/static/'}),

Set padding on numbers using Django's template engine

{{ variable|stringformat:"02d" }}

Get your python path

$manage.py shell >>>import sys >>>sys.path

Generic view with extra context

When using generic views with an extra context as in the example below the following contexts will be available in the template.

def resellers(request, mystate, lecodeer): return object_list(request, queryset=Resellers.objects.filter(state=mystate.upper(), suburb__istartswith=lecodeer).order_by('suburb','agent'), template_name='reseller_list.html', extra_context={'blah': mystate }, )

Context, object_list

{% for reseller in object_list %} {{ reseller.suburb }} {% endfor %}

Context, blah

{{blah}}

Python *args and **kwargs special characters in a function definition

Being new to Python and looking at the following code, I could not understand what values were positional arguments and what were keyword arguments, so I wrote the function below to tell me.
Function Call:
render_response('index.html', {'param1': 'paramvalue'}, context_instance='RequestContext(request)', response='myreponse')
Function Definition:
def render_response(*args, **kwargs):
So i wrote the following function definition to test my theory:
>>> def render_response(*args, **kwargs): print 'ARGS: ' + str(args) + '\n' + 'KWARGS: ' + str(kwargs) Test input:
>>> render_response('index.html', {'param1': 'paramvalue'}, context_instance='RequestContext(request)', response='myreponse') Result:
ARGS: ('index.html', {'param1': 'paramvalue'}) KWARGS: {'context_instance': 'RequestContext(request)', 'response': 'myresponse'} From looking at the result of the function it is clear that dictionary data types are intercodeted as positional arguments.

Handy Javascript for anchor tag

onmouseout="this.bgColor='white'" onmouseover="this.bgColor='yellow'" onclick="this.bgColor='white'"

Can't get past main page in Django admin site

Check that there is no trailing $ in urlconf for the admin site. You can login and access the main page on the admin, however clicking on table will throw a 404 error.

Secodeing Custom Cookies

Set a cookie on a response object using the render to response. Saves a lot of coding.

response = render_to_response(foo) response.set_cookie(bar ...) return response