Django OldForms Automatic Form Generation
As of May 2007 Django NewForms doesn't yet deal with File and Image Fields properly. The NewForms library doesn't validate or generate HTML forms for File uploads or similarly Image uploads.
In the meantime OldForms library can, however the OldForms lack the handy feature of outputting the entire form as HTML into your template. Instead of the old way of creating a variable for each element in your form in your template, essentially coding the entire form by hand, Newforms can print out the entire form as HTML formatted string directly to your template with the methods form.as_p or form.as_ul and form.as_table. This method described below is less elegant than custom laying out each form specifically for each instance, but if you have many models that each need a unique form created, this hand coded process can be tedious. So the following ugly bit of code uses Django's OldForms but generates HTML like NewForms can for an arbitrary model specified by "model_name" that is taken from the URL as specified in your urls.py.
For example a line in your urls.py might look like this:
...
(r'^appname/(?P<model_name>[0-9A-Za-z-]+)/create/$', 'projectname.appname.views.create'),
...
and here is the view that the above code calls:
from django.shortcuts import render_to_response, get_object_or_404
from projectname.appname.models import *
from django import oldforms
def create(request, model_name,):
"""function to automatically generate html for Django's OldForms.
Has an argument, model_name, that accepts a string of the name
of the model you want to create a form for."""
model_name = is_model(model_name)
model = eval(model_name)
manipulator = model.AddManipulator()
if request.method == 'POST':
new_data = request.POST.copy()
from django.db import models
if model._meta.has_field_type(models.FileField):
new_data.update(request.FILES)
errors = manipulator.get_validation_errors(new_data)
manipulator.do_html2python(new_data)
if not errors:
new_obj = manipulator.save(new_data)
return HttpResponseRedirect( new_obj.get_absolute_url() )
else:
errors = new_data = {}
def form_to_html(form):
"""Takes a Django OldForm object as an argument and spits out an html form
along with any related validation errors"""
formHtml = []
from django.db import models
for i in form.manipulator.opts.fields + form.manipulator.opts.many_to_many:
if not type(i) == models.AutoField:
try:
if type(i) == models.FileField or type(i) == models.ImageField:
formHtml += ["<label for='id_%s' >%s:</label>%s %s %s"
%(i.name,
i.name.capitalize(),
form.__getitem__(key=i.name).field_list(),
form.__getitem__(key=i.name+'_file').field_list(),
form.__getitem__(key=i.name).error_list ) ]
else:
formHtml += ["<label for='id_%s' >%s:</label>%s %s"
%(i.name, i.name.capitalize(),
form.__getitem__(key=i.name).field_list(),
form.__getitem__(key=i.name).error_list ) ]
except:
pass
return formHtml
form = oldforms.FormWrapper(manipulator, new_data, errors)
formHtml = form_to_html(form)
return render_to_response(
"object_form.html",
{"formHtml": formHtml, "model_name": model_name})

Comments
You can handle upload using custom views, it is not very difficult. The oldforms form engine will be removed in the next releases, it's better to don't use it
I understand that the oldforms will soon be obsoleted, if not already, however this was more a hack spurred on by my laziness. From what I saw at the time, several people had expressed confusion as to how to handle this situation. For my situation, I thought it easier to make the oldforms library emulate newforms' automatic form formatting and output than it was to make newforms handle file and image uploads the way oldforms does.
comments have been disabled until I get a captcha setup to stop the comment spam.