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.):
Rules:
- 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.
- Post these rules, the source code for your solution, and the following list (with you included) on your blog.
- Bonus points if you implement it in a language not yet seen on the following list!
The List:
- [Python] http://www.eflorenzano.com/blog/post/trying-start-programming-meme
- [Bash] http://aartemenko.com/texts/bash-meme/
- [C] http://dakrauth.com/media/site/text/hello.c
- [Java] http://adoleo.com/blog/2008/nov/25/programming-meme/
- [Python 3] http://mikewatkins.ca/2008/11/25/hello-meme/
- [Ruby] http://stroky.l.googlepages.com/gem
- [Ruby] http://im.camronflanders.com/archive/meme/
- [Lisp] http://justinlilly.com/blog/2008/nov/25/back-on-the-horse/
- [JavaScript] http://www.taylanpince.com/blog/posts/responding-to-a-programming-meme/
- [Django] http://www.pocketuniverse.ca/archive/2008/november/27/florenzano-factor/
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
TEMPLATE_DEBUG = DEBUG
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = ':memory:'
TEMPLATE_DIRS = (
'./',
)
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)
else:
form = NameAgeForm()
return HttpResponse(render_to_string('settings.py', locals()))
urlpatterns = patterns('',
(r'^$', view),
)
INSTALLED_APPS = ()
# {% 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 settings.py and run:
django-admin.py 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 - pocketuniverse.ca


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:
django-admin.py starproject univ
which generated the appropriate directory and files. Then I replaced the settings.py that django generated with the one that you provided on this page.
Then I used your command:
django-admin.py 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.)