How can I modify the class for a button in a django widget form? -
i'm trying set class form widget. have:
<form action="{% url "list" %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <p>{{ form.non_field_errors }}</p> <p>{{ form.input.label_tag }} {{ form.input.help_text }}</p> <p> {{ form.input.errors }} {{ form.input }} </p> <p>{{ form.non_field_errors }}</p> <p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p> <p> {{ form.itinerary.errors }} {{ form.itinerary }} </p> <p><input class="button-primary" type="submit" value="upload" /></p> </form>
and looks
i attempted use django tweak tools when tried googling find solution problem. modified code see how change:
<form action="{% url "list" %}" method="post" enctype="multipart/form-data"> {% csrf_token %} {% load widget_tweaks %} <p>{{ form.non_field_errors }}</p> <p>{{ form.input.label_tag }} {{ form.input.help_text }}</p> <p> {{ form.input.errors }} {{ form.input }} {% render_field form.input class="button" %} </p> <p>{{ form.non_field_errors }}</p> <p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p> <p> {{ form.itinerary.errors }} {{ form.itinerary }} </p> <p><input class="button-primary" type="submit" value="upload" /></p> </form>
and looks
it applies class whole widget, , turns whole widget button.
so how can apply class button in widget?
so there isn't easy way add class form element.
the way able solve create custom template tag.
if interested in learning more can visit following link: https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/
what did create directory under app directory called templatetags.
make sure have __init__.py
in directory, , create file. named mine template_helper.py
# template_helper.py django import template django.utils.datastructures import sorteddict register = template.library() @register.filter(name='addcss') def addcss(field, css): return field.as_widget(attrs={"class":css})
in actual template have first load template_helper via: {% load template_helper %}
add {{ form_element|addcss:"button" }}
you'll want somehow check template tags
{% if ... %}
make sure apply class button if want apply class button.
{% load template_helper %} <form action="{% url "list" %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <p>{{ form.non_field_errors }}</p> <p>{{ form.input.label_tag }} {{ form.input.help_text }}</p> <p> {{ form.input.errors }} {% field in form.input %} {{ field|addcss:"button" }} {% endfor %} </p> <p>{{ form.non_field_errors }}</p> <p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p> <p> {{ form.itinerary.errors }} {{ form.itinerary }} </p> <p><input class="button-primary" type="submit" value="upload" /></p> </form>
good luck!
Comments
Post a Comment