3

I have created models and from the models, I created ModelForms. However, I noticed that the widgets generated from the ModelForm are not as same as the widgets when I am in Admin pages.

How can I:

  1. Get the same widget as the ones in admin pages. Those are awesome. The foreign key field comes with 'plus' icon that enables you to add new item being referenced to.

  2. Date time widget has 'Now' button that enables you to populate the field instantly with the current time.

  3. Is there any other cool plugin out there for the widgets? So far all I got is meh, just plain html widget that doesnt make sense even for field like Datetime (it generates normal plain input control).

Alexey Savanovich
  • 1,893
  • 11
  • 19
Haikal Nashuha
  • 2,958
  • 7
  • 44
  • 67
  • http://stackoverflow.com/questions/38601/using-django-time-date-widgets-in-custom-form The answer for this one explains what you need. If you figure out what widgets are you will be able to write your own, it's really easy – Dima Bildin Jan 20 '12 at 09:15

1 Answers1

6

You should have a look to django/contrib/admin/widgets.py You will find here the admin site widgets. Then in you form, make something like:

from django.contrib.admin.widgets import AdminDateWidget

class MyForm(ModelForm):

    class Meta:
        widgets = {
            'date': AdminDateWidget(),
        }

Make sure that your template includes the required media files (css, js)

I also recommend to look at django-floppyforms which is a very nice lib if you want better widgets

luc
  • 41,928
  • 25
  • 127
  • 172
  • just a quick question, does floppyforms have feature like the admin ( now for datetime field, auto add button for foreign key etc) – Haikal Nashuha Jan 21 '12 at 09:02
  • There is a date widget. See https://github.com/brutasse/django-floppyforms/blob/master/docs/widgets.rst. I think I never used this one but It worths a look. – luc Jan 23 '12 at 08:14