Bootstrap Multiselect Integration with WTForms - python

I'm new to WTForms and was wondering how to integrate a bootstrap styled multiselect like this one http://davidstutz.github.io/bootstrap-multiselect/ into WTForms.
I know I can write the HTML directly to create the multiselect dropdown form but I'd rather work with WTForm objects to keep my forms consistent. Is there a simple way to convert the WTForms SelectMultipleField class into a dropdown multiselect box? Or is something more sophisticated needed?
Thanks!

I'd recommend a hybrid approach: Write the JavaScript yourself but let WTForms do the HTML part: Create a normal SelectMultipleField and on rendering call it like this: form.select_multiple(class_='multiselect'). Because the plugin itself just required valid HTML and something to select such a select field to apply its JavaScript part (which you can include on the page).
Of course, if you want more advanced stuff, you can always subclass the widget for the SelectMultipleField and extend it. Then use this as the custom widget (For example on the iconized part of the examples you linked).
Really, on the part of WTForms you don't need much modification.

Related

Enable wagtail DateField when a wagtail BooleanField is true

I need to have a wagtail DateField disabled by default, but if the content author checks a box (a wagtail BooleanField) then the field should be enabled and required. I'm struggling to find the best way to solve this, I haven't found documentation on how to do this. I was thinking about using Django signals or wagtail hooks but it does seem like a complex solution for what I think should be a common use case. So I was wondering if anyone has a better alternative or point me to the right direction.
I don't believe this is possible currently in Wagtail (as of Wagtail 2.2). Not natively, at least.
What you can do, however, is add custom JavaScript to your admin with a snippet like this:
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.utils.html import format_html
from wagtail.core import hooks
#hooks.register("insert_global_admin_js", order=100)
def global_admin_js():
"""Add custom.js to Wagtail Admin."""
return format_html('<script src="{}"></script>', static("js/custom.js"))
And then inside your custom.js file, you can add a JavaScript event to detect when the boolean field is checked or not, and then find and select the DateField and disable or hide it.
As Willem Van Onsem has mentioned, you'll want to make the DateField nullable and blank so you can save the Page (as a form) when the DateField is empty.

Pre-check Django admin checkboxes

Consider a Django Admin changelist view looking somewhat like this:
I would like to pre-check some of the checkboxes in the list. Documentation didn't help me further.
Any ideas on how to achieve this in a nice way? Where by nice I mean, as standard as possible.
You could tick the checkboxes using JavaScript. For example, to tick the checkbox for the item with primary key 1, using jQuery, you would do:
$(".action-select[value='1']").prop('checked', true);
Or you could use Django's bundled jQuery if you want:
django.jQuery(".action-select[value='1']").prop('checked', true);

Django: Defining custom fields to work with Angular Material

I'm working on an existing Django project to upgrade it's GUI to Angular Material. I've used Django forms and model forms throughout the project and now it's very difficult to write the Angular Material like custom tags and widgets in the templates to replace each form. So, I was trying to write some code which can covert all my default Django form fields to Angular Material tags.
I found two ways of doing so:
Writing a new render function say as_md() with a custom def _html_output(), similar to as_ul() or as_p() by
inheriting Form/ModelForm class. This will convert the complete form fields to the new material tags, however this is a less flexible way (in case I want to change the layout for some of the forms).
Writing custom widgets to render each field as per my requirement.
This will require writing custom widget for each field type.
Can there be some other way to implement this in a more efficient way? Any pointers will be highly appreciated.
Custom Fields
If you need the two way data binding, your fields will each need an ng-model to keep the binding intact. Writing custom widgets will help you achieve this.
But to make sure all your forms will use these custom widgets, a custom render function will work great.
django-angular
If you're looking for something with less hassle, take a look at the django-angular project, it aims to achieve the same thing.

How do I use wtforms RadioField input to add an option with open-ended text input option?

How can I use wtforms / flask_wtf to render an HTML form:
with a select field that gives the responder fixed choices, plus
an "Other" option that allows them to enter text.
I'm using the RadioField to get the particular choices, but I don't see how to add an open ended text box to the selection choices.
Is there any way to do this without hacking the code?
It's basically s SelectField plus a TextField.
You just need to handle that on the server side. If you want, you can use some javascript in order to show/hide this extra textfield using your radio button.
Information about the different widgets available here: http://wtforms.simplecodes.com/docs/0.6.1/widgets.html

Add custom Django admin action

I'm looking for ways of adding custom actions to a Django admin change page. I am not looking for actions that can be added to the dropdown box in the overview.
One model in my Django application contains information about documents, and those are automatically compiled to PDF files at the frontend. I'd like to give the administrator the possibility to quickly render the PDF directly from the change page to be able to quickly check the results.
I've already played around with overriding change_form.html/submit_line.html, and it's absolutely no problem to add a button. But I wonder how to extend the admin module in a clean way so that it includes my custom action.
Since custom admin views are basically just normal views there aren't many specialities. A few things you should consider for a cleaner integration:
Add the url pattern through get_urls() of the AdminSite.
Provide the current_app argument to RequestContext or Context when rendering a template which subclasses admin templates.
EDIT:
Found a an example here:
http://shrenikp.webs.com/apps/blog/show/5211522-add-custom-view-method-for-django-admin-model-
Note, that the example doesn't utilize the current_app argument i've mentioned. I suppose your view to generate the PDF just returns a HttpResponse with the appropriate content type rather than rendering a response with a Context, so it's not needed. All in all the current_app only makes sense when you subclass an admin template used by your custom view which actually makes use of current_app somewhere.
The example encapsulates the urls and view in a ModelAdmin. It is also possible to do this through a AdminSite subclass, but at least in your use case this is probably overkill.
By the way, overriding the change_form.html template for your app to add a button to the standard change view is just fine. There is no special api for this in the admin (unfortunately).

Categories

Resources