Recently in Django Category

Stupefying Django error

| | No TrackBacks

Wow. I spent a lot of time on this this afternoon. I had a Django application with an admin interface. Every time I tried to create an object with no foreign keys, I got this error on the save:

Traceback (most recent call last):

  File "C:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 279, in run
    self.result = application(self.environ, self.start_response)

  File "C:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 651, in __call__
    return self.application(environ, start_response)

  File "C:\Python26\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "C:\Python26\lib\site-packages\django\core\handlers\base.py", line 134, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "C:\Python26\lib\site-packages\django\core\handlers\base.py", line 154, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 40, in technical_500_response
    html = reporter.get_traceback_html()

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 86, in get_traceback_html
    frames = self.get_traceback_frames()

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 205, in get_traceback_frames
    pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 186, in _get_lines_from_file
    context_line = source[lineno].strip('\n')

IndexError: list index out of range

Totally mysterious: there is not a line of my code in the trace stack and I have no idea what file it is trying to index into. I really made no progress on this until I tried the shell:

>>> from django_league.league.models import *
>>> Sport.objects.create(sport_name='Hockey')
__unicode__
Traceback (most recent call last):
  File "lgt;console>", line 1, in lgt;module>
  File "C:\Python26\lib\site-packages\django\db\models\base.py", line 328, in __
repr__
    u = unicode(self)
  File "C:\Users\hughdbrown\Documents\django\django_league\..\django_league\leag
ue\models.py", line 17, in __unicode__
    return u"%s" % (sport_name, )
NameError: global name 'sport_name' is not defined

Now we're getting somewhere, I thought. I have a function of mine in scope. The problem was that I had defined my __unicode__ method without using self in the code:

class Sport(models.Model):    
    sport_name = models.CharField(max_length=20)
    def __unicode__(self):
        return u"%s" % (self.sport_name, )

    @models.permalink
    def get_absolute_url(self):
        return ('sport', None, {'object_id' : self.id})

    class Meta:
        ordering = ['sport_name']

So fixing it was pretty easy once I knew that.

Open source components

| | No TrackBacks

Last week, I went to a django presentation at HUGE Inc. in Brooklyn. For me, the highlight was Kevin Fricovsky's five minute (well, ten minute) lightning talk in which he demonstrated the power of using open source components. He took a fairly small blog project of his and pulled it down from github.com. Then he used pip to get the dependencies listed in the requirements.txt file. Inside of ten minutes, he had a working blog that used five to ten open source components.

In contrast to this, I got a big reality check on using open source code. I wanted to try out some of Steve Souders's ideas on improving website performance on a django open source project that is already running. I forked some code from github and was going to apply django-compressor to the CSS and JS to see the performance improvement. I never got to this point.

The project was missing a couple of important pieces:

  • a list of the requirements and how to obtain them
  • the settings.py file to list the installed apps

Without these, I was pretty much dead. I soldiered on, though, and found eight required components to add, but:

  • one of the components did not work with python 2.5 at head rev
  • I got a subtle templetags error message that I eventually realized meant that I had a wrong similarly-named open source component

I've written to the project author to find out where to get the correct component. It's a bracing counterpoint to the presentation. Taking your own code and adding components -- likely to work. Taking a project in an unknown state and trying to back out what the components are -- much more likely to fail.

By the way, if you get a message like this:

'XXX' is not a valid tag library: Could not load template library from django.templatetags.XXX, No module named models

then I have a technique for you. Assuming that templatetags module XXX has a ton of import statements in its files, you have to figure out which file it is importing that is causing the failure. Take code like this:

from django import template
from django.conf import settings
from django.core import template_loader
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.utils.safestring import mark_safe
from syncr.twitter.models import Tweet
from syncr.delicious.models import Bookmark
from syncr.flickr.models import Photo
from tagging.templatetags import tagging_tags
from tumblr.models import TumblrPost
from lastfm.models import LastfmPost
import re
import datetime

and turn it into this:

try:
    from django import template
    from django.conf import settings
    from django.core import template_loader
    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    from django.core.exceptions import ObjectDoesNotExist
    from django.utils.safestring import mark_safe
except ImportError, exc:
    print "django error in %s: %s" % (__file__, exc)

try:
    from syncr.twitter.models import Tweet
    from syncr.delicious.models import Bookmark
    from syncr.flickr.models import Photo
    from tagging.templatetags import tagging_tags
    from tumblr.models import TumblrPost
    from lastfm.models import LastfmPost
except ImportError, exc:
    print "package error in %s: %s" % (__file__, exc)

try:
    import re
    import datetime
except ImportError, exc:
    print "python error in %s: %s" % (__file__, exc)

Do this for every file in the module. Run the dev server and watch for the import error location reported. Once you know which block is failing in which file, add print statements to see which particular import you get to before it fails. Then you'll know which import is not working. In my case, this showed me that I had used the wrong open source tumblr module for this project.

James Bennett has some more detailed observations on this point, too.

Recent reading

| | No TrackBacks

I've been doing a lot of reading recently. Here are Django books I've finished:

Here are general web development books I've read:

And a book on Git: Version Control with Git.

In addition, here are the books I am currently reading:

I particularly recommend Pro Django for django developers. It is advanced and offers a lot of insight into django and the python techniques it is built on.

Django

| | No TrackBacks

Recently, I've gone crazy for Django. It's a fabulous platform for developing web applications (primarily content management systems) really fast. I love it!

How to use Django with Apache and mod_python

Serving media files

Django cheatsheets

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Archive

This page is an archive of recent entries in the Django category.

dancing is the previous category.

ebay is the next category.

Find recent content on the main index or look in the archives to find all content.