Archive for the ‘django’ Category

Comfortable development mode in Django

Friday, July 28th, 2006

If you run django in development mode, you need to tell it to serve static files by itself. If you use django in production mode, your http-deamon should handle that for you. You can automate the switchover by an appropriate urls.py setting:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
    (r'^admin/',    include('django.contrib.admin.urls')),
    (r'^r/',       include('django.conf.urls.shortcut')),
    (r'^comments/', include('django.contrib.comments.urls.comments')),
[...]
)

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P.*)$',
            'django.views.static.serve', {'document_root': '../media'}),
    )

Saturday, July 22nd, 2006

Setting the Last-Modified on a page served by django is not elegant but necessary for good HTTP-Citizenship.

An approach to this problem is like this:

def homepage(request):
  latest_news = NewsItem.objects.order_by('-created_at')[:5]
  response = render_to_response('news/homepage.html',
    {'latest': latest_news}, context_instance=RequestContext(request))
  response['Last-Modified'] = \
    latest_news[0].created_at.strftime('%a, %d %b %Y %H:%M:%S GMT')
  return response

Getting the right bins of the shelf

Saturday, July 8th, 2006

I currently have the pleasureof writing a warehouse managment system (using Djangos OR mapper – in case you care).

The warehouse has the concept of bins (e.g. Pallet) containing products. A bin typically contains between 1 (think fitness bike) and 3000 (think dart flys) products. Of the same product there are bins with different quantities of products available. The total number of bins with a product can be up to several hundreds.

One of the non-trivial issues is choosing the bins which allow to fullfill an order without repacking. So lets say I get an order for 300 Skateboards. I have one Pallet with 100 Skateboards, 3 with 80, 1 with 45, 3 with 25, 3 with 10, 2 with 2 and 2 with 1. This can be modelled as a list: [100, 80, 80, 80, 45, 25, 25, 25, 10, 10, 10, 2, 2, 2, 2, 2, 1, 1]

Turns out a quite simple and reasonable fast algorythm can find an acceptable solution:

>>> def find_fitting(items, n):
...   def _fittings(items, n):
...     for i in xrange(len(items)):
...       if items[i] == n:
...         yield [items[i]]
...       if items[i] <= n:
...         for cc in _fittings(items[:i]+items[i+1:],\
                n - items[i]):
...           yield [items[i]]+cc
...
...   items.sort()
...   items.reverse() # we prefer bigger bins
...   if sum(items) >> find_fitting([100, 80, 80, 80, 45, 25, 25, 25, \
                  10, 10, 10, 2, 2, 2, 2, 2, 1, 1], 300)
[100, 80, 80, 25, 10, 2, 2, 1]

Django “forgot your password”

Sunday, June 18th, 2006

Django has a nice but hidden Password reset feature. To use it, you have to uncomment the “forgotten your password?” link in the login form. This patch is an example how to do it.

Than you have to activate it in your main urls.py like this:

(r'^password_reset/$',     'django.contrib.auth.views.password_reset'),
(r'^password_reset/done/$','django.contrib.auth.views.password_reset_done'),

You’re basically set here, although there are still some rough edges. One thing is that the emails sent out with the new password have some issues. One is variable substitutiuon -this patch (Ticket #2186) fixes it.

Another issue are that unwanted links might creep on your “password successfully reset” page. This patch fixes this issue.

Django comments

Tuesday, June 13th, 2006

Django contains a wonderful comment framework. It is underdocumented but very nice. You can see it at work on the Django Website where it powers the comments function on many of the pages.

When you post a comment, the comment framework provides you with a “thank you for your comment, back to the original page” webpage. One thing I was missing there is that the “back to the original page” uses an anchor tag to link to the position of the new comment on the original page.

This patch provides this ability. It is Ticket #2134 at the django trac.

Fixing display of redirects in the admin interface

Tuesday, June 13th, 2006

This tiny patch makes redirects in the magic-removal admin interface of django look nice as they did in pre magic-removal times.

It’s Ticket #2139 at django and has been fixed in Changeset 3122.

Generating PDFs in Django

Sunday, June 4th, 2006

Try a view like this to get PDFs out of Django:

from reportlab.pdfgen.canvas import Canvas

def fixplatzfahnen(request):
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'inline; filename=fixplatzfahnen.pdf'

pdf = Canvas(response)
pdf.drawString(10, 10, "TEST")
pdf.showPage()
pdf.save()
return response