Django add attribute to individual radio or checkbox - django

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.

Related

Implement a typeahead for manytomany field

I am looking for a robust solution to implement a typeahead (Twitter typeahead) for a manytomany field. Basically, something identical to the tag input field here in StackOverflow.
The default widget for manytomany is a multiselect. However, since I want the user to provide new values, I need to use a inputText widget. My question is, what would be the best way to implement this functionality so that I can later pass an array of models instances to a cleaning stage?
In my cleaning stage I plan on doing a loop through the elements to check if they exist in the db, create them if they don't and apply validators to each elements.
My initial intuition was to use a hidden field that would receive the actual fields from the typeahead via javascript manipulation. Thus the input field would not be part of the model, just serve as an input box for the user.
Why reinvent the wheel. You can simply use django-taggit together with selectize.js. By using both of them, you don't even need any customization.

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.

Conditional fields using web forms for marketers?

I need to show/hide a field based on the value entered in another field. The field that is conditionally made visible also needs to be a mandatory field.
Is this possible using WFFM? Will I need to do it using custom fields?
Thanks!
You would need to build a custom complex type to do this. You might be able to re-use existing field types in your custom type, but it would probably be simpler to create them specifically for the new complex type.
You can use JQuery for this and also you can create custom field for this you just simply copy and paste previous one and reflect Sitecore.Forms.Core.dll and see the code how you can achieve this. But I generally use JQuery if I can manage via this

TinyMCE plugin custom tags in Django

I am building a custom image insert plugin for TinyMCE. The idea is that each article already has a relationship with a collection of images through an ArticleImage model which allows the user to provide an article-specific caption. The TinyMCE will then insert a custom tag (something like <myapp:image image-id="9389" caption="Caption override">) which is rendered as a preview of the image and caption in the editor, and rendered into <figure><img src="images/9389.jpg" /><figcaption>Caption override (Photo: photographer)</figcaption></figure>. This could equally be something like <myapp:poll> or <myapp:video>.
My question is: what is the best way (and where is the best place) to parse this 'dummy tag' into its rendered HTML in the Django view?
Or is there another, better approach?
IMHO, the best place to render custom markup, is in the template via a templatefilter.
I would risk myself to say that using a templatefilter to render custom markup is the "djangoish" way, since that's the way to go with django.contrib.markup.
Storing the custom tag in the model is a good idea, because then you can change the template filter implementation, which would be impossible if the custom tag is processed before storage.

Django - Add class to individual rows in a ModelChoiceField?

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.