Archive for the 'django' Category

Send javascript errors by mail

I'm running a Django-powered site for a closed user group and added a bit of JavaScript magic here and there (mainly Prototype and Tooltip).

Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has solved this problem but didn't find anything, so here's my take: Just send any error using Ajax (here: using Prototypes Ajax abstraction) to the server

JAVASCRIPT:
  1. onerror = Extranet.mailError;
  2. function mailError(msg, url, line) {
  3.   var postBody = 'url=' + url + '&line=' + line + '&message=' + escape(msg) + '&useragent=' + escape(navigator.userAgent) + '&user=' + escape(user_name);
  4.   var myAjax = new Ajax.Request('/api/jserror/', {method: 'post', postBody: postBody});
  5. }

user_name is a JavaScript variable holding the Django username (so I know whom I can inform when the error is fixed).

On the server side, I just send me mails containing the JavaScript error message, the username and the user agent:

PYTHON:
  1. def jserror(request):
  2.   from django.core.mail import mail_admins
  3.   omit_messages = ['pointerobj is not defined', 'tipobj is not defined', 'ns6 is not defined', 'enabletip is not defined']
  4.   if request.POST.get('message', '') not in omit_messages:
  5.     message = """url: %s (%s)
  6. %s
  7. user-agent: %s
  8. username: %s
  9. """ % (request.POST.get('url', ''), request.POST.get('line', ''), request.POST.get('message', ''), request.POST.get('useragent', ''), request.POST.get('user', ''))
  10.     mail_admins("javascript error", urldecode(message))
  11.   return HttpResponse()

Yeah, that's all very trivial but I wonder what other solutions exist for this problem...

Django: Serve big files via fcgid

I've got a django project running which requires you to login to access files.
That means that I have to serve the files via python, like this:

PYTHON:
  1. @login_required
  2. def download(request, filename):
  3.     # ... some code specific to my site ...
  4.     response = HttpResponse(mimetype=postUpload.mimetype)
  5.     response['Content-Disposition'] = "attachment; filename=" + original_filename
  6.     response['Content-Length'] = os.path.getsize(filename_path)
  7.     response.write(open(filename_path).read())
  8.     return response

The problem: If the download of a file exceeded 5 minutes (big files and/or low bandwidth) the download was canceled on the server side by a timeout. This Apache configuration for mod_fcgid solved the problem (see mod_fcgid documentation for BusyTimeout)

CODE:
  1. <IfModule mod_fcgid.c>
  2.   BusyTimeout 1200
  3. </IfModule>

The problem was that the apache module scanned every minute for processes that run for more than BusyTimeout seconds. These processes are potentially in bad health (infinite loop et al.) and have to be killed. Not so with my processes (since I know what I'm doing..). The setting of the busy timeout to 1200 seconds now lets my processes run for a maximum of one hour.

As this setting can't by overwritten in a htaccess file by default I needed to bug my web hosting provider with the request, which was handled in 24 hours, so thanks for that one!

PS: If you know of another way how to serve protected static files via a single sign on (no HTTP basic auth), please let me know.

Using djangos newforms

If you start a django-app just now (January 2007) you are in a dilemma since using the old form engine is discouraged:

If you're starting from scratch, we strongly encourage you not to waste your time learning this

But then, the documentation of new form engine, called "newforms", isn't complete at all.

You may argue «take a framework above 1.0», and you'd probably be right. As I wanted to stick with django, I implemented form handling the hard way by figuring out how to use newforms.

Anyway, as I finally got formhandling working with newforms, I thought I'd share this with you, my fellow readers..

The following code..

screenshot of the form
this examples' form after validating user input
  • ..displays a form
  • ..validates user input
  • ..shows validation errors
  • ..writes the user data into the database

(more...)