how to use django widget tweaks and combine template class string and widget attr attributes string name
I am trying to customise a django form for use with bootstrap 4, custom html layout & per field class or id names on the FormModel defintion
I have the following html
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
And the following form defintion:
class DocumentForm(forms.ModelForm):
field1 = PartLookupField(required=True, widget=forms.TextInput(attrs={'class': 'field1-choice-ajax'}))
field2 = forms.CharField(required=True, widget=forms.TextInput(attrs={'id': 'field2-field'}))
form_lines = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Document
fields = ("field1", "field2", "form_lines")
So essentially, I need to get the per field definition of id or class, from the widget on the model, and combine that with the template defined form-control or is-valid/invalid classes in the template.
I've tried going down this route
How to concatenate strings in django templates?
But it just seems like it's going to end up in a huge mess.
Essentially, how can I combine template defined attributes and per field defined attributes? I need to end up with class="form-control field1-choice-ajax" for the field specified in the model (and the correct extra class names for the valid/invalid states).
Previously I was using the bootstrap4 form library, but I need complete control now:
{% csrf_token %}
{% bootstrap_form form %}
html django django-widget-tweaks
add a comment |
I am trying to customise a django form for use with bootstrap 4, custom html layout & per field class or id names on the FormModel defintion
I have the following html
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
And the following form defintion:
class DocumentForm(forms.ModelForm):
field1 = PartLookupField(required=True, widget=forms.TextInput(attrs={'class': 'field1-choice-ajax'}))
field2 = forms.CharField(required=True, widget=forms.TextInput(attrs={'id': 'field2-field'}))
form_lines = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Document
fields = ("field1", "field2", "form_lines")
So essentially, I need to get the per field definition of id or class, from the widget on the model, and combine that with the template defined form-control or is-valid/invalid classes in the template.
I've tried going down this route
How to concatenate strings in django templates?
But it just seems like it's going to end up in a huge mess.
Essentially, how can I combine template defined attributes and per field defined attributes? I need to end up with class="form-control field1-choice-ajax" for the field specified in the model (and the correct extra class names for the valid/invalid states).
Previously I was using the bootstrap4 form library, but I need complete control now:
{% csrf_token %}
{% bootstrap_form form %}
html django django-widget-tweaks
add a comment |
I am trying to customise a django form for use with bootstrap 4, custom html layout & per field class or id names on the FormModel defintion
I have the following html
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
And the following form defintion:
class DocumentForm(forms.ModelForm):
field1 = PartLookupField(required=True, widget=forms.TextInput(attrs={'class': 'field1-choice-ajax'}))
field2 = forms.CharField(required=True, widget=forms.TextInput(attrs={'id': 'field2-field'}))
form_lines = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Document
fields = ("field1", "field2", "form_lines")
So essentially, I need to get the per field definition of id or class, from the widget on the model, and combine that with the template defined form-control or is-valid/invalid classes in the template.
I've tried going down this route
How to concatenate strings in django templates?
But it just seems like it's going to end up in a huge mess.
Essentially, how can I combine template defined attributes and per field defined attributes? I need to end up with class="form-control field1-choice-ajax" for the field specified in the model (and the correct extra class names for the valid/invalid states).
Previously I was using the bootstrap4 form library, but I need complete control now:
{% csrf_token %}
{% bootstrap_form form %}
html django django-widget-tweaks
I am trying to customise a django form for use with bootstrap 4, custom html layout & per field class or id names on the FormModel defintion
I have the following html
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
And the following form defintion:
class DocumentForm(forms.ModelForm):
field1 = PartLookupField(required=True, widget=forms.TextInput(attrs={'class': 'field1-choice-ajax'}))
field2 = forms.CharField(required=True, widget=forms.TextInput(attrs={'id': 'field2-field'}))
form_lines = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Document
fields = ("field1", "field2", "form_lines")
So essentially, I need to get the per field definition of id or class, from the widget on the model, and combine that with the template defined form-control or is-valid/invalid classes in the template.
I've tried going down this route
How to concatenate strings in django templates?
But it just seems like it's going to end up in a huge mess.
Essentially, how can I combine template defined attributes and per field defined attributes? I need to end up with class="form-control field1-choice-ajax" for the field specified in the model (and the correct extra class names for the valid/invalid states).
Previously I was using the bootstrap4 form library, but I need complete control now:
{% csrf_token %}
{% bootstrap_form form %}
html django django-widget-tweaks
html django django-widget-tweaks
edited Nov 21 '18 at 11:14
Chris Barry
asked Nov 21 '18 at 11:08
Chris BarryChris Barry
2,00563977
2,00563977
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've created my own template filter in order to add class attributes to existing form fields:
@register.filter
def add_class(field, css):
"""Add a class to a field in a template.
Example:
> {{ form.my_field|add_class:"required" }}
<input id="my_field_id" name="my_field" class="required" type="text">
Args:
field: this should be a form field, of type ``BoundField``
css: this should be a string with one or more class names separated by spaces
"""
class_old = field.field.widget.attrs.get('class', None)
class_new = class_old + ' ' + css if class_old else css
return field.as_widget(attrs={'class': class_new})
So now I can do this in a template:
{{ field|add_class:"is-valid" }}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410813%2fhow-to-use-django-widget-tweaks-and-combine-template-class-string-and-widget-att%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've created my own template filter in order to add class attributes to existing form fields:
@register.filter
def add_class(field, css):
"""Add a class to a field in a template.
Example:
> {{ form.my_field|add_class:"required" }}
<input id="my_field_id" name="my_field" class="required" type="text">
Args:
field: this should be a form field, of type ``BoundField``
css: this should be a string with one or more class names separated by spaces
"""
class_old = field.field.widget.attrs.get('class', None)
class_new = class_old + ' ' + css if class_old else css
return field.as_widget(attrs={'class': class_new})
So now I can do this in a template:
{{ field|add_class:"is-valid" }}
add a comment |
I've created my own template filter in order to add class attributes to existing form fields:
@register.filter
def add_class(field, css):
"""Add a class to a field in a template.
Example:
> {{ form.my_field|add_class:"required" }}
<input id="my_field_id" name="my_field" class="required" type="text">
Args:
field: this should be a form field, of type ``BoundField``
css: this should be a string with one or more class names separated by spaces
"""
class_old = field.field.widget.attrs.get('class', None)
class_new = class_old + ' ' + css if class_old else css
return field.as_widget(attrs={'class': class_new})
So now I can do this in a template:
{{ field|add_class:"is-valid" }}
add a comment |
I've created my own template filter in order to add class attributes to existing form fields:
@register.filter
def add_class(field, css):
"""Add a class to a field in a template.
Example:
> {{ form.my_field|add_class:"required" }}
<input id="my_field_id" name="my_field" class="required" type="text">
Args:
field: this should be a form field, of type ``BoundField``
css: this should be a string with one or more class names separated by spaces
"""
class_old = field.field.widget.attrs.get('class', None)
class_new = class_old + ' ' + css if class_old else css
return field.as_widget(attrs={'class': class_new})
So now I can do this in a template:
{{ field|add_class:"is-valid" }}
I've created my own template filter in order to add class attributes to existing form fields:
@register.filter
def add_class(field, css):
"""Add a class to a field in a template.
Example:
> {{ form.my_field|add_class:"required" }}
<input id="my_field_id" name="my_field" class="required" type="text">
Args:
field: this should be a form field, of type ``BoundField``
css: this should be a string with one or more class names separated by spaces
"""
class_old = field.field.widget.attrs.get('class', None)
class_new = class_old + ' ' + css if class_old else css
return field.as_widget(attrs={'class': class_new})
So now I can do this in a template:
{{ field|add_class:"is-valid" }}
answered Nov 21 '18 at 11:32
dirkgrotendirkgroten
4,51311221
4,51311221
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410813%2fhow-to-use-django-widget-tweaks-and-combine-template-class-string-and-widget-att%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown