> hi
> I have a model with 3 fields
> class Employee(models.Model):
> name=models.CharField(max_length=50)
> home_address=models.TextField(blank=True)
> change_filter=models.IntegerField()
>
> I am creating a modelform using this class as model.
> class EmployeeForm(ModelForm):
> class Meta:
> model=Employee
>
> In my template,I want to give the IntegerField some special
> treatment ..ie,I want to render it as input type="range" instead of a
> text field.
> I can do this like,
> ...
> <p>
> <label for="id_name">Enter name:</label>
> {{empform.name}}
> </p>
> <p>
> {{empform.name.errors}}
> </p>
> <p>
> <label for="id_home_address">Enter address:</label>
> {{empform.home_address}}
> </p>
> <p>
> {{empform.home_address.errors}}
> </p>
>
> <p>
> <label for="id_change_filter">select change filter value:</label>
> <input type="range"
> min="0"
> max="1000"
> step="2"
> value="6" id="id_change_filter" name="change_filter"/>
> </p>
> <p>
> {{empform.change_filter.errors}}
> </p>
>
> <input type="submit" value="Submit"/>
> </form>
>
> Here ,I am using the same logic for the first two fields..
> I want to render the change_filter using an input type="render"..
> so I thought I can take care of this using a for loop.
> {% for field in empform %}
> {% ifequal field.name "change_filter" %}
> <p>
> <label for="id_change_filter">select change filter value:</
> label>
> <input type="range"
> min="0"
> max="1000"
> step="2"
> value="6" id="id_change_filter" name="change_filter"/>
> </p>
> {% else %}
> <p>
> {{ field.label_tag }}: {{ field }}
> </p>
> {% endifequal %}
> {%if field.errors %}
> {{field.errors}}
> {% endif %}
> {% endfor %}
>
> This gives the desired effect..But I am wondering if hardcoding the
> field's name in the ifequal tag is the correct way.Can I check the
> type of field instead?I am not sure how to do this..
> Any advice would be nice..
> thanks
> jim
Don't do this in the template. Define a custom widget for your field,
which outputs the range input. Look at the code for the standard
widgets in django.forms.widgets to see how to do it.
--
DR.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment