The Florenzano Factor

Eric Florenzano (A Django Wizard) has instigated a meme (Update: Now with URL back to his post. Guh. I'm a yutz.):


  1. Implement a program that takes in a user's name and their age, and prints hello to them once for every year that they have been alive.
  2. Post these rules, the source code for your solution, and the following list (with you included) on your blog.
  3. Bonus points if you implement it in a language not yet seen on the following list!

The List:

  1. [Python]
  2. [Bash]
  3. [C]
  4. [Java]
  5. [Python 3]
  6. [Ruby]
  7. [Ruby]
  8. [Lisp]
  9. [JavaScript]
  10. [Django]

Yes, friends, that's right. I did it in Django. But not only that, I did it in one file in Django.

Dig it:

{# #} {% comment %}
from django import forms
from django.conf.urls.defaults import *
from django.http import HttpResponse
from django.template.loader import render_to_string
DEBUG = True
DATABASE_NAME = ':memory:'
ROOT_URLCONF = 'settings'
class NameAgeForm(forms.Form):
name = forms.CharField()
age = forms.IntegerField()
def view(request):
if 'name' in request.GET:
form = NameAgeForm(request.GET)
if form.is_valid():
name = form.cleaned_data['name']
ages = range(1, form.cleaned_data['age'] + 1)
form = NameAgeForm()
return HttpResponse(render_to_string('', locals()))
urlpatterns = patterns('',
(r'^$', view),

# {% endcomment %} <html><head><title>The Age Machine</title></head><body>{% if name and ages %} {% comment %}
# {% endcomment %} <ul>{% for age in ages %}<li>{{ age }} - Hello {{ name }}</li>{% endfor %}</ul> {% comment %}
# {% endcomment %} <form action="/"><input type="submit" value="Reset" /></form> {% comment %}
# {% endcomment %} {% else %}<form method="GET" action="/">{{ form.as_p }}<input type="submit" /></form> {% endif %}</body></html>

To run it, just put this code in a file called and run: runserver --settings="settings"

I managed to get everything crammed into the settings file, and then I made the settings file double as the template. You're either going to be really impressed or really angry. Let me know, either way.

sam - at -

Being new to Django (but not new to programming) I liked the idea of a project that does a lot with very little code.

So I wanted to get this working.

But I kept getting an error about not being able to import the settings file. Maybe you have some idea of what I'm doing wrong.

I started with this: starproject univ

which generated the appropriate directory and files. Then I replaced the that django generated with the one that you provided on this page.

Then I used your command: runserver --settings="settings"

I received the error about not being able to import the settings file, so after googling around a bit, I created an environment variable in Windows called PYTHONPATH to point to my django-apps folder which contains my univ app. Then I created a DJANGO_MODULE_SETTINGS environment variable and set it to univ.settings.

No luck.

I deleted the DJANGO_MODULE_SETTINGS and tried pointing the PYTHONPATH variable directly at the django-apps/univ folder instead of the folder one level up.

Still no luck.

By the way, I'm using xp, django 0.96 and Python 2.5 - don't know if that makes any difference. If you have any suggestions, I would appreciate it - thought this looked pretty cool and would love to get it running.

— Wilcox (December 11, 2008 at 2:27 p.m.)

Hey Wilcox,

You shouldn't need to start a projct or set PYTHON_PATH or anything.

If the django-admin command isn't working, you could try running python in the dir that is in. You should be able to "import settings" from the python shell. That would at least tell you if python can find the file.

— Sam (December 12, 2008 at 9:59 a.m.)

Yes, python can find the file. I deleted the PYTHONPATH variable and the DJANGO_SETTINGS_MODULE as you suggested. I put your in a directory with nothing else in it.

When I do "import settings" from the python shell, python finds but I get a traceback which indicates that the environment variable DJANGO_SETTINGS_MODULE is not defined. (The traceback is down below)

Then I tried dozens of variations of adding and removing different folders from the DJANGO_SETTINGS_MODULE and PYTHONPATH variables to see if I could get it to work but nothing seemed to help.

Any suggestions would be appreciated.


import settings Traceback (most recent call last): File "[HTML_REMOVED]", line 1, in [HTML_REMOVED] File "", line 3, in [HTML_REMOVED] from django import forms File "C:\Python25\Lib\site-packages\django\", line 1, in [HTML_REMOVED] from django.oldforms import * File "C:\Python25\Lib\site-packages\django\", line 1, in [HTML_REMOVED] from django.core import validators File "C:\Python25\lib\site-packages\django\core\", line 13, in [HTML_REMOVED] from django.utils.translation import gettext, gettext_lazy, ngettext File "C:\Python25\Lib\site-packages\django\utils\", line 3, in [HTML_REMOVED] if settings.USE_I18N: File "C:\Python25\Lib\site-packages\django\", line 28, in getattr self._import_settings() File "C:\Python25\Lib\site-packages\django\", line 53, in _import_settings raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

— Wilcox (December 14, 2008 at 3:47 p.m.)

How peculiar.

I just tried copying the code of the post into, and it worked for me.

I'm sorry for playing the OS card, but I wonder if this is a different between Windows and Ubuntu.

I'd be happy to continue troubleshooting this with you. Drop me a line. sam -at-

— Sam (December 17, 2008 at 10:57 a.m.)