[英]How can I modify the class for a button in a django widget form?
I'm trying to set the class for a form widget. 我正在尝试为表单窗口小部件设置类。 I 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 it looks like 它看起来像
I attempted to use django tweak tools when I tried googling to find a solution for my problem. 当我尝试使用Google搜索来查找问题的解决方案时,尝试使用django调整工具。 I modified the code to be like this too see how it would 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 that looks like 看起来像
It applies the class to the whole widget, and it turns the whole widget into a button. 它将类应用于整个小部件,并将整个小部件变成一个按钮。
So how can I apply the class to just the button in the widget? 那么如何将类仅应用于窗口小部件中的按钮?
So there really isn't an easy way to add a class to a form element. 因此,确实没有简单的方法可以将类添加到表单元素。
The way I was able to solve this was to create a custom template tag. 我能够解决此问题的方法是创建一个自定义模板标签。
If you interested in learning more about this you can visit the following link: https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/ 如果您想了解更多有关此的信息,可以访问以下链接: https : //docs.djangoproject.com/en/1.7/howto/custom-template-tags/
What I did was create a directory under the app directory called templatetags. 我要做的是在应用程序目录下创建一个名为templatetags的目录。
Make sure you have an __init__.py
in this directory, and then create another file. 确保此目录中有一个
__init__.py
,然后创建另一个文件。 I named mine template_helper.py 我命名为mine template_helper.py
# template_helper.py
from django import template
from django.utils.datastructures import SortedDict
register = template.Library()
@register.filter(name='addcss')
def addcss(field, css):
return field.as_widget(attrs={"class":css})
In the actual template you would just have to first load the template_helper via: {% load template_helper %}
then add the {{ form_element|addcss:"button" }}
在实际的模板中,您只需要首先通过以下方式
{% load template_helper %}
: {% load template_helper %}
然后添加{{ form_element|addcss:"button" }}
You'll want to somehow check with the template tags
{% if ... %}
to make sure you only apply the class to the button if you only want to apply the class to the button.您需要以某种方式检查模板标签
{% if ... %}
,以确保仅在将类应用于按钮时才将类应用于按钮。
{% 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 }}
{% for 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! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.