views:

84

answers:

2

Hi,

I am a current web2py user, but find I still go back to Django once in a while (where I started). Specifically when working on projects where I want to make use of some specific django apps/plugins/extensions that don't yet exist in web2py.

One thing that I can't live without in web2py, which I am looking for a solution for in Django, is the way to create html forms from a db table and being able to then customize their look and layout in the view, without javascript.

Key things I am looking for:

  1. Generate html form from a db table
  2. Assign custom css classes/ids to each field in the generated html form (js disabled)
  3. Place each form field/element in a pre-made html view via a method call in the view

i.e.

I have a table A. In web2py I can do (in controller):

def display_form():
   form = SQLFORM(db.table_A)
   #Can I do the following in Django? Assign custom CSS to each form field?
   form.element(_name='email')['_class'] = = "custom_css_classes, list"

   if form.accepts(request.vars, session):
       response.flash = 'form accepted'
   elif form.errors:
       response.flash = 'form has errors'
   else:
       response.flash = 'please fill out the form'
   return dict(form=form)

Then, in the View I can do:

form.custom.start
form.custom.widget.name
form.custom.widget.email
form.custom.widget.form_field_name
           ...
<div class="span-5 last"><input type="submit" class="register_btn" value="Sign Up"></input></div>
form.custom.end

The above takes a DB table, creates an HTML form, and then lets me stick each separate form field in any place in the pre-made HTML that I want (using those "custom" method calls on the passed "form" object. Including the custom css classes I assigned to each separate field of the generated html form.

See documentation for details on the above code:

http://web2py.com/book/default/chapter/06?search=define_table

http://web2py.com/book/default/chapter/07?search=sqlform#SQLFORM

http://web2py.com/book/default/chapter/05?search=#Server-side-DOM-and-Parsing

http://web2py.com/book/default/chapter/07?search=form.custom

How do I do the above in Django without dirtying my javascript with layout hacks. Assume javascript is disabled in the browsers where I need my app to run. Furthermore, I would love to make use of Django admin. Pylons solutions also welcome!

Links to articles/tutorials/howtos for this would be greatly appreciated. Also, please make an equivalent result of the above code using the method you mention in your response...

+1  A: 

If you haven't already, take a look at Django's ModelForm. I am assuming that you have models mapped to the tables in question. Vanilla ModelForm instances will work without JS. However ModelForms are usually defined ahead of time and not constructed on the fly. I suppose they can be created on the fly but that would be a bit tricky.

Manoj Govindan
Couldn't see anything in the documentation about specifying css for the generated form fields...did I miss it somewhere?
watr
+1  A: 

Use ModelForm and override any field you wanna customize by explicitly declaring them.

If you want to set field attributes like class and id, you need to do something like this:

name = forms.CharField(
                widget=forms.TextInput(attrs={'class':'special'}))

In case you are interested, you may change the order of the fields by specifying a fields sequence in your Meta class:

class Meta:
    model = YourModel
    fields = ('title', 'content')

You may read the full documentation here: http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs

Satoru.Logic
watr
You can do exactly the same with web2py in one line: db.mytable.myfield.widget=lambda f,v:SQLFORM.widgets.string.widget(f,v,_class='special')
mdipierro
And didn't you notice that I break the line of code into two line just for readability?
Satoru.Logic