Forms issues

12 views
Skip to first unread message

bernz

unread,
Dec 7, 2009, 9:47:27 PM12/7/09
to dojango-users
Hi all,

I've been trying to use the new Forms Integration. I'm following the
docs that are on this site (http://code.google.com/p/dojango/wiki/
FormIntegration) but I'm not really getting anywhere.

I'm used to doing forms like this (in forms.py)
----
from django import forms as forms

class BasicUserAttributesForm(forms.Form):
user_role = forms.CharField(label="Please pick your role.",
max_length=10, widget=forms.Select(choices=ROLE_CHOICES, attrs=
{'onchange':'get_newform();'}))
----

But dojango doesn't fit that paradigm, which is fine. What would be
the best way to express that in dojango?

I tried:
----
from dojango.forms.models import ModelForm as DojoModelForm
from dojango.forms import fields, widgets

class BasicUserAttributesForm(DojoModelForm):
user_role = widgets.CharField(label="Please pick your role.",
max_length=10, widget=widgets.Select(choices=ROLE_CHOICES, attrs=
{'onchange':'get_newform();'}))
----

But I get an "NoneType" error when I call it in a view.

Do you have more thorough docs on how to use forms with dojango or any
pointers you can give?

Thanks for this great software. I think it'll change how I develop.

David

klipstein

unread,
Dec 8, 2009, 3:23:55 AM12/8/09
to dojango-users
Hi David,

I've just tried your example and here it works, if you are using
fields.CharField instead of widgets.CharField. For further
investigation I would need a traceback of that error. But as it seems
it is not a problem of dojango.

It is important, that you enable the middleware
'dojango.middleware.DojoCollector' before using the form integration.
For further documentation you should have a look at the forms
documentation of Django (http://docs.djangoproject.com/en/dev/#forms)
and the form documentation of Dojo (http://livedocs.dojotoolkit.org/
dijit/form).

Regards, Tobias

David Bernick

unread,
Dec 8, 2009, 8:26:50 AM12/8/09
to dojang...@googlegroups.com
Here's my forms.py:

from dojango.forms.models import ModelForm as DojoModelForm
from dojango.forms import fields, widgets

class BasicUserAttributesForm(DojoModelForm):
user_role = fields.CharField(label="Please pick your role.",
max_length=10, widget=widgets.Select(choices=ROLE_CHOICES,
attrs={'onchange':'get_newform();'}))

Here's my views.py:

@login_required
def attributes_wizard_index(request):
u = g_user(request.user)
form = BasicUserAttributesForm()
return render_to_response('authn/attributes_wizard_index.html', {
'role_form':form,
'g_user':g_auth(request.user)}
)

Traceback:
Environment:

Request Method: GET
Request URL: http://localhost:8000/authn/attributes_wizard/
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'emailauth',
'rbac',
'dojango',
'geriatric',
'geriatric.authn',
'geriatric.invite']
Installed Middleware:
('dojango.middleware.DojoCollector',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/auth/decorators.py"
in __call__
78. return self.view_func(request, *args, **kwargs)
File "/home/bernz/workspace/geriatric/geriatric/../geriatric/authn/views.py"
in attributes_wizard_index
32. form = BasicUserAttributesForm()
File "/usr/lib/pymodules/python2.6/django/forms/models.py" in __init__
218. self.instance = opts.model()

Exception Type: TypeError at /authn/attributes_wizard/
Exception Value: 'NoneType' object is not callable
--
Hacker for hire.
ineedahacker.com

klipstein

unread,
Dec 8, 2009, 8:39:37 AM12/8/09
to dojango-users
Hi David,

now I see your problem. You shouldn't use a modelform if no model is
attached to the form :)

# in this case dojango.forms.Form == django.forms.Form
# and you just will use the dojango form widgets/fields
from dojango.forms import Form as DojoForm
class BasicUserAttribuesForm(DojoForm):
...

if you want to use modelforms a model needs to be attached to your
form (see http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#topics-forms-modelforms),
e.g.:

from dojango.forms.models import ModelForm as DojoModelForm
class BasicUserAttributesForm(DojoModelForm):
class Meta:
model = models.User

This will autogenerate the form with the information from your model.

Regards, Tobias

David Bernick

unread,
Dec 8, 2009, 9:16:00 AM12/8/09
to dojang...@googlegroups.com
That did it! Thanks!

So now all of my fields are Dojo-fied.

I've used Dojo (and jquery and prototype) in my templates before
plenty of times. I was hoping that Dojango would give me greater
integration than just having Dojo in templates. So far so good.

Is there any documentation about all of the things that dojango can
do? I admit that I find the Wiki site a little lacking in that regard.
How can I best get oriented with the library?

klipstein

unread,
Dec 8, 2009, 3:10:15 PM12/8/09
to dojango-users
Hi David,

I would say that nearly all components of dojango are documented. In
general you need to know, that dojango is not trying to do any big
magic and generating JavaScript code out of python for you. So this is
why a lot of documentation can be found at Django or within the Dojo
documentation and in the dojango wiki you just find the differences.

Of interest could be, that I soon will finish a dojango sample
application, where you can see the usage of all the dojango parts.
Maybe this could be a place, where you can find the information you
are looking for. But any concrete input on unclear/uncomplete
documentation would be great.

Regards, Tobias

David Bernick

unread,
Dec 8, 2009, 3:40:45 PM12/8/09
to dojang...@googlegroups.com
> Of interest could be, that I soon will finish a dojango sample
> application, where you can see the usage of all the dojango parts.
> Maybe this could be a place, where you can find the information you
> are looking for. But any concrete input on unclear/uncomplete
> documentation would be great.

Actually, that's exactly the kind of thing I'm looking for. Just
something that shows how you envision its general usage.

David Bernick

unread,
Dec 8, 2009, 3:50:48 PM12/8/09
to dojang...@googlegroups.com
> I would say that nearly all components of dojango are documented. In
> general you need to know, that dojango is not trying to do any big
> magic and generating JavaScript code out of python for you. So this is
> why a lot of documentation can be found at Django or within the Dojo
> documentation and in the dojango wiki you just find the differences.

i admire the goals you have stated on the site and I think they're
helpful for the Django community, but your form/widgets are, to me,
the stars. That kind of "ease of use" is a tremendous help. While it
doesn't quite allow for "wavemaker" style development, it certainly
speeds up dev. Are there plans for more features along those lines?

klipstein

unread,
Dec 8, 2009, 4:03:17 PM12/8/09
to dojango-users
Yes. There are several plans for the future. Still need to update the
roadmap:

* build-profile-generation
* implemenation of json-rpc / json-rest (via django-piston)
* combination of those interfaces with grid / charting
* templates for dijit.layout
* keeping up-to-date with dojo form elements
* usage of dojox.form.Form vs. dijit.form.Form
* django-admin-interface built with dojo

These are the things that come in my mind at the moment. If you have
other suggestions, let me know.

Greetings, Tobias
Reply all
Reply to author
Forward
0 new messages