Nov

27

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:

  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] http://www.eflorenzano.com/blog/post/trying-start-programming-meme
  2. [Bash] http://aartemenko.com/texts/bash-meme/
  3. [C] http://dakrauth.com/media/site/text/hello.c
  4. [Java] http://adoleo.com/blog/2008/nov/25/programming-meme/
  5. [Python 3] http://mikewatkins.ca/2008/11/25/hello-meme/
  6. [Ruby] http://stroky.l.googlepages.com/gem
  7. [Ruby] http://im.camronflanders.com/archive/meme/
  8. [Lisp] http://justinlilly.com/blog/2008/nov/25/back-on-the-horse/
  9. [JavaScript] http://www.taylanpince.com/blog/posts/responding-to-a-programming-meme/
  10. [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.)

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 settings.py 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 settings.py in a directory with nothing else in it.

When I do "import settings" from the python shell, python finds settings.py 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.

Thanks.

import settings Traceback (most recent call last): File "[HTML_REMOVED]", line 1, in [HTML_REMOVED] File "settings.py", line 3, in [HTML_REMOVED] from django import forms File "C:\Python25\Lib\site-packages\django\forms__init.py", line 1, in [HTML_REMOVED] from django.oldforms import * File "C:\Python25\Lib\site-packages\django\oldforms__init.py", line 1, in [HTML_REMOVED] from django.core import validators File "C:\Python25\lib\site-packages\django\core\validators.py", line 13, in [HTML_REMOVED] from django.utils.translation import gettext, gettext_lazy, ngettext File "C:\Python25\Lib\site-packages\django\utils\translation__init.py", line 3, in [HTML_REMOVED] if settings.USE_I18N: File "C:\Python25\Lib\site-packages\django\conf__init.py", line 28, in getattr self._import_settings() File "C:\Python25\Lib\site-packages\django\conf__init__.py", 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 settings.py, 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- pocketuniverse.ca

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