Django - Add class to individual rows in a ModelChoiceField? - django

I'm using a model choice field in one of my forms and I would like to add a CSS class to each option based on a value of an attribute of the model.
Is there an easy way to do this?
Any advice appreciated.
Thanks.

Two options:
You'll have to render the select yourself in the template, iterating over {{ form.field_name.choices }} and generating the class.
Create a custom Widget which extends django.forms.widgets.Select and overrides it's render_option() method. For example how it's implemented, see render_option() in django's source. Use the widget in the form (as the field's widget).
You can see another example of something similar (disabled options in that case) in django snippets: Allow disabling options in a select widget.
IMO the 2nd option is cleaner: it's reusable and no need to mess with the templates.

Related

How to customize the fields order and widget using the default templates in grails 3?

I would like to set the order of the fields displayed in the default templates of grails 3 (using the f:all, f:table tags etc). I would also like to specify the widget to use for the field without specifying a different template for it (I know about http://grails-fields-plugin.github.io/grails-fields/guide/customizingFieldRendering.html and have created some custom templates for other fields). For example to just use TextArea rather than Text field. Can it somehow be controlled by the settings or contraints from the Model?
Thank you.
The order of the constraints determines the order of the scaffolding. To specify a textarea intead of input you can specify a widget:
description widget: 'textarea'
Some documentation here.

Django add attribute to individual radio or checkbox

I'm using a ModelForm to create multiple choice questions, and I want to apply a CSS class to a particular option (the "other" choice, in my case).
Is there a simple way to do this? It seems like it could theoretically be done with a custom widget, but I haven't been able to make a functional custom widget.

Sitecore 6 WFFM: How to hide fields?

In Web Forms for Marketers, I have a few fields that I am using to pass values to the pipeline processors and to append CSS code to the form.
I don't want these fields to appear on the form. How can I mark a field to be invisible? Right now I am doing this via CSS, but I am quite sure there must be a better way to do so.
Thanks!
You can extend the Single Line Text field class and change the container CSS class to your own CSS class that hides the field and label. You can also add value to that field when the form is loaded through the querystring.
I believe there is a Hidden Field type in WFFM. Could you use that?

Twitter Bootstrap's ".input-prepend" with a Django Form

I'm new to Django and have been using django-bootstrap-form. I've been pretty happy with it, but I don't believe it provides a way to format the output for ".input-prepend" as described in Twitter Bootstrap's docs.
I'm guessing I'm going to have to override my input field's widget for this particular field. I'm not sure of the best way to do this, though. Any help would be appreciated.
You'll have to edit the templates, adding an if clause for prepending and possibly two more for appending or both cases at once or handle this with a templatetag (or beter even, the widget render method).
Have a look what's there to get the hang of it.
For the field itself either create a custom formfield or create a widget by wrapping a basic widget.

Django Model field as formfield in templates

I guess this is a simple question, but I am not being able to find a definitive answer whether this is possible or not.
I have a Model object that is being passed to my template during rendering. While I use most of the Model fields to show their values, I came across a need to show a few of them as formfields (with their respective HTML element like rendered via ModelForms). Can ModelForm class usage be avoided here and simply use the Model's field to be rendered as formfield directly from template?
an example:
class MyModel(models.Model):
month = models.PositiveIntegerField(_('Month'), choices=MONTH_CHOICES)
year = models.PositiveIntegerField(_('Year'))
Now in template based on the above Model example I want to show the year as a value but month as a dropdown with selected current value?
Any pointer appreciated!
Use case:
Imagine a generic list_view like case where you are rendering a queryset. What you have there is object per iteration, however on the list view you want to allow your users to perform some quick editing of few object attributes to avoid forcing them to go to full edit mode (maybe via simple ajax).
No.
As canvassed in the comments, this is not possible. A model, and its fields do not know about forms - that is what forms, and modelforms are for.
The right way to do this is to wrap your model objects with a modelform, which you can adjust (using inheritance) to do whatever you want with the non-form fields, if you wish to make this more transparent.
Creating modelforms in the template means that you have nowhere to process the forms in django. Apparently you want to interface them with some kind of ajax interface, so perhaps this is not a problem for you, but in any case, I fail to see how it would be easier than simply creating the modelforms in the view.