Using Datepicker in Django

I am using Django and Datepicker together with Twitter-Bootstrap and I want the form.fields to have same class and same id if they are of same widget(DateInput). In specific, i would like to change the id accordingly. It should have a different id every time.

This is the class i defined below. How do i change the id to be a different one every time.

class MyDatePicker(forms.DateInput)
  def __init__(self, *args, **kwargs):
      attrs = kwargs.pop("attrs",{})
      attrs["class"] = "datepick" 
      attrs["id"] ="date_1"
      kwargs["attrs"] = attrs
      super(MyDatePicker, self).__init__(*args, **kwargs)

How do i go about doing it?Need some help on it...Would welcome any suggestion...

Answers


You could register a templatetag to find out the widget type:

@register.filter('klass')
def klass(obj):
    return obj.__class__.__name__

Then in your template:

{% load your_filter_module %}
<form id="form" enctype="multipart/form-data" class="form-horizontal" method="POST" action="">{% csrf_token %}
    <fieldset>
        {{ form.non_field_errors }}
        {% for field in form %}
            <div class="control-group{% if field.errors %} error{% else %}{% if form_errors %} success{% endif %}{% endif %}">
                <label class="control-label" for="{{ field.auto_id }}">{% trans field.label %}</label>
                <div class="controls">
                    {% if field.field.widget|klass == "CheckboxInput" %}
                        <label class="checkbox">
                            <input type="checkbox" id="{{ field.auto_id }}" name="{{ field.name }}"{% if field.value == "on" %} checked{% endif %}>
                            {% trans "Some random text" %}
                        </label>
                    {% endif %}
                    # ... etcetera continue check for other classes ...

Note: A cleaner DRY approach would be to add the loop and class logic (horizontal/vertical etc) to a filter returning a html widget


Need Your Help

Git-svn: create & push a new branch/tag?

git git-svn

After cloning an SVN repository using git-svn with the -s option (git svn clone http://server/repo -s), how does one create a branch or tag and have pushed to the relevant branch/tag directory in the