How to change booleanfields using bootstrap switches? - django

I want to use the switch button on the bootstrap to operate the model called field, but I don't know how to access it.
class NewBlank(models.Model):
...
blank_on_off = models.BooleanField(default=False)
...
If I have the following booleanfield and can I change the booleanfield by cycling the switch as shown in the picture?

From the HTML specifications, the default value for a checkbox is "on" because you have not included a value attribute
default/on
On getting, if the element has a value attribute, it must return that
attribute's value; otherwise, it must return the string "on". On
setting, it must set the element's value attribute to the new value.
Change your html to
<div class="bootstrap-switch-square">
<input type="checkbox" data-toggle="switch" name="Resend" id="Resend" value="true" />
</div>
so that if its checked, a value of true will be sent in the form data and correctly bound to your boolean property (if unchecked, no value will be sent and the property will be its default (false) value
originaly answered

Related

How to render default values in django-ajax_select field

I am using ajax_select for my m2m and foreign key fields, its working fine but it is not rendering default value of that field, it is rending empty value ("|").
I am not using ajax_select in my admin panel, so when I open that form in admin panel, fields are having default values already. That means their is no problem in default values but in ajax_select field.
What it is rending now :
<input type="hidden" name="colours" id="id_colours" value="|" data-ajax-select="autocompleteselectmultiple" data-plugin-options="{"source": "/ajax_select/ajax_lookup/colours", "html": true}" data-changed="true">
What I wanted is:
<input type="hidden" name="colours" id="id_colours" value="|8|" data-ajax-select="autocompleteselectmultiple" data-plugin-options="{"source": "/ajax_select/ajax_lookup/colours", "html": true}" data-changed="true">
If at least default value initialized in name=colour, I can show help text that default value is White.
As I searched at documentation of ajax_select but nothing found related to it, does anyone know how to render default values in ajax_select field.
Is this problem occurring with me only or ajax_select doesn't have this default value feature ?
This can be achieved by overriding the get_form() method as :
def get_form(self,form_class=None):
form = super().get_form(form_class)
form['colours'].initial = '8'
return form
Now default value is set manually and also working in ajax_select.

Passing the complete Object Instance, NOT just the object id, from a Django template to view

I am trying to pass full object instances from my django template to my view as the values associated with checkbox inputs. When I try to do so, I only get the object's unicode method representation and am thus unable to access any of the object's particular fields in my view.
Is there either a way to pass the actual object from the template to the view or, instead, a way to use that unicode representation in the view to access the actual object and its corresponding fields?
I don't want to use 'publication.id' as my checkbox value because I can't guarantee the ids will never change.
Current code:
mytemplate.html
<form method="get" action="processData">
{% for publication in mydata %}
<input type="checkbox" name="item-selection" value="{{publication}}">
{% endfor % }
view.py
items = request.GET.getlist('item-selection')
for x in items:
print x # this prints the unicode mehtod's return value
x.datafield1 # I want to access each object's data
No, this can't possibly work: sending data from the browser to the view is done via HTTP post, and you can only post data, you can't post objects.
But you've said the solution yourself: use the IDs. Who cares if the IDs change? All that matters is that you get the ID as it currently is.
<input type="checkbox" name="item-selection" value="{{publication.id}}"><label>{{ publication.name }}</label>
...
item_ids = request.GET.getlist('item-selection')
items = Item.objects.filter(id__in=item_ids)

How to access drop down list field type selected value in sitecore

I can access Single-Line text field type by following in Repeater:
<sc:FieldRenderer ID="frTitle" runat="server"
FieldName="Title"
Item="<%# (Sitecore.Data.Items.Item)Container.DataItem %>" />
but how to access drop-down list field type selected value defined in item.
Thanks
This depends on the exact field type you are using.
If using a Droplist field type
The value is stored in Sitecore as plain text representing the name* of the selected item. In this case you could use your code sample to render out the name of the selected item (if that's really what you want to do). *Note that the content editor will see the Display Name of the items in the droplist, but your code would render out the item name. Using this field type is not usually a good idea as it is not possible to translate item names.
If using a DropTree or Droplink field type
The value is stored in Sitecore is an ID of the linked item. In this case you would need to get the selected item using this ID, then render out the required field of that item. I would probably write a code-behind method to get the selected item, then call this method within your FieldRenderer. Something like this:
Code-behind:
public Item GetLinkedItem(Item item, string itemField)
{
string dropDownItemId = item[itemField];
return Sitecore.Context.Database.GetItem(dropDownItemId);
}
Ascx markup:
<asp:Repeater ID="rptChildren" runat="server">
<ItemTemplate>
<sc:FieldRenderer ID="frTitle" runat="server"
FieldName="Title"
Item='<%# GetLinkedItem((Sitecore.Data.Items.Item)Container.DataItem, "YourDropLinkFieldName") %>' />
</ItemTemplate>

Django forms: checkboxes are displayed without value

I'm unable to save my form (a ModelForm) properly, since django displays checkboxes without a value (I would expect value="true" on every fields, both checked than unchecked... but that's not the case).
When I submit the form, no data is received in the POST!
The following is a piece o my template:
<div>
{{form.displayAge.label_tag}}
{{form.displayAge}}
{{form.displayAge.errors}}
</div>
{{form.displayAge}} is rendered in this way:
<input checked="checked" type="checkbox" name="displayAge" id="id_displayAge">
BUT... since it has no value, checking/unchecking the checkbox is helpless! What should I do?
I would like to avoid typing form fields by hand!
No, there is no need for a value field. If the checkbox is checked, the browser will submit "on" as the value by default if none is supplied.
If you're not getting this value in your view, something else is wrong. Note that since you're using Django forms, you shouldn't be checking request.POST manually anyway: use form.cleaned_data.

Get Value Of Selected Option ModelChoiceField

Without using Ajax is there a way to get the value of a selected item. So for example, if I have the below drop down list:
<select name="controllers" id="id_controllers">
<option value="" selected="selected">---------</option>
<option value="1">http://przemeklach.com/api/firstOrder/przemeksController</option>
<option value="5">http://przemeklach.com/api/zeroOrder/ronsController</option>
</select>
How would I get at the 'value' in my view. I know I can get the 'http://przemeklach.com/api/firstOrder/przemeksController' part via
controller = form.cleaned_data['controllers']
but I also need the 'value' in this case 1.
Thanks.
Scratch the old response (below), cleaned_data contains object references. You can get the ID by refering to model methods.
You can get the id from form.data['controllers'] but it needs sanity checking (in this case it should be an int). Of course if the is_valid() returns True it should be one of the ids available in the queryset you supplied when defining the field.