How can I replace a select box in django with buttons using bootstrap? - django

I am working on a ticketing system and a create new ticket form which requires you to enter the status (working, open, closed etc.), the severity (low, high, normal, asap etc.) and several other parameters. The select boxes seem to be old-school and time-consuming so I want to replace them with a series of buttons.
model
status = models.CharField(max_length=100, choices=STATUS_CHOICES)
severity = models.CharField(max_length=100, choices=SEVERITY_CHOICES)
template:
<div class="form-group">
{{ form.status.errors }}
<label for="id_status" >Status:</label>
{{ form.status }}
</div>
<div class="form-group">
{{ form.severity.errors }}
<label for="id_severity" >Severity:</label>
{{ form.severity }}
</div>
How can this be achieved?

I'm assuming you meant radio buttons here, in which case you need to change the form widget for the field:
SEVERITY_CHOICES=[('critical','Critical'),
('blocking','Blocking),
('normal','Normal)]]
severity = forms.ChoiceField(choices=SEVERITY_CHOICES, widget=forms.RadioSelect())
If that's not what you're after then you'll have to write your own custom widget.

Button is not meant for that, man.
I commented your question and what i meant was - HOW - in the sense of HTML, can BUTTON be used for storing selected value? Button is not meant for that. Button is meant for clickin and doing something upon click.
Yes - you can make button click open drop-down list of elements. And yes- twitter bootstrap supports that (http://getbootstrap.com/components/#btn-dropdowns).
But what does not work is:
Button does not store the selected value. You would have to write all that js by yourself - change button inner text to indicate selected value.
Even worse - BUTTON is not an html element with value attribute - and it does not get posted on form post. You would have to come up with some means of inserting that value into list of values that get posted on form submit. All possible if you know your javascript.
There is no support in django for presenting field with many values as button. You would have to create your own widget for that. Again - it is all possible with django.
BUT... Why go through all that work, when, with some CSS you could just make your select LOOK like the button styles that Twitter bootstrap offers....

Related

Django: Update specific part of template by button click

I have a text field
<input type="text" value="This is a test." name="mytextbox" size="10"/>
and separated from that a button
<button type = "button" class="btn btn-primary">
<font size="1">Run</font>
</button>
that should update a list and update the value of the text field itself.
Sometimes forms are used in this context, but I would have to wrap the forms tag around the whole template when the input and button are separated on the screen.
Since I am relatively new to Django, I would like to know the best
strategy to solve the problem.
a) Reload the complete page template with changed arguments/context
b) Create a html template of the specific part that extends the main template and only try to render/update this one.
c) Do something smarter.
There are some answers in a (much older) post from 8 years ago but I am interested in the state-of-the-art solution.

Capybara save_and_open_page doesn't reflect radio buttons tate

I am trying to select the radio button for the freelancer the code is as follows (when we inspect the element on browser)
<label for="registration_payer_type_business"><input checked="checked" id="registration_payer_type_business" name="registration[payer_type]" type="radio" value="business">
Company
</label>
<label for="registration_payer_type_freelancer"><input id="registration_payer_type_freelancer" name="registration[payer_type]" type="radio" value="freelancer">
Freelancer
</label>
I have tried
page.choose("registration_payer_type_freelancer")
This doesn't give any error but when and save and open page in capybara still the radio box is not selected against freelancer. I would appreciate if people can give example using xpath and choose.
The real issue you're most likely having is that save_and_open_page saves the HTML with the current attribute values NOT the current property values . This means the fact that you've selected a radio button (which changes the checked property value, not attribute value, won't necessarily be shown). You're better off using save_and_open_screenshot if you want to see the current state of a page. That being said below is ways you can select radio buttons.
To select a specific radio button with Capybara you can use the id, name, label text, and value too if needed to make unique (with name for instance)
choose('registration_payer_type_freelancer') # id
choose('registration[payer_type]', option: 'freelancer') # name and value to make unique
choose('Freelancer') # label text
choose(option: 'freelancer') # just value if the only radio button with that value
In all those cases, if the actual radio button input element is non-visible (for styling purposes, etc) on the page and you want to instead click the visible label you can pass allow_label_click: true
choose('registration_payer_type_freelancer', allow_label_click: true) # find by id and select by clicking the label if input is non-visible
Other options you could use is to just find by CSS (the :css argument can be ignored if your default selector type is the default :css)
find(:css, '#registration_payer_type_freelancer').click
You could also use XPath queries to locate the element, but they're really not necessary 98% of the time (more people correctly understand CSS and with scoping of finders it can generally be used to get any element), and have issues to be aware of - https://github.com/teamcapybara/capybara/blob/master/README.md#beware-the-xpath--trap
find(:xpath, './/input[#value="freelancer"]').click

Sitecore Web Forms for Marketers, add info to the display-section-legend class

We are on Sitecore 8 using Web Forms for Marketers.
I am trying to identify how to add information to the "display-section-info" class item in a sitecore WFFM form. Looking # The generated code I see an element (display-section-info class) after the Field Legend, and before the starts of our fields. I would like to put some basic information regarding the fields in this element (below has text "THIS IS WHERE I WOULD LIKE TO ADD TEXT").
Here is the source from "View Source" on the browser. Through developer tools I plugged in some info and that is exactly where I want it to go.
<fieldset class="display-section-fieldset">
<legend class="display-section-legend">1. OUTSIDE INTEREST:</legend>
<p class="display-section-info">THIS IS WHERE I WOULD LIKE TO ADD TEXT</p>
<div class="display-section-content">
<div class=" field-border">
<span class=" field-title">
<span class=" field-required">*</span>
In the field below, list exceptions
</span>
Update1:
per Jammycans response I added a few parameters to the section but did not seem to display. items have been published, I also confirmed on the prod DB.
Content Editor
Results:
Thanks in advance
There is no field in the Form Editor to set this information, you can set it directly on the section item itself.
In the Content Editor, expand the form and select the Form Section item. On the section item in the Parameters field set the information field text you need:
<Information>THIS IS WHERE I WOULD LIKE TO ADD TEXT</Information>
You can use the Localized Parameters field if you need to translate the text.
EDIT:
There is a bug in the logic on the default WFFM section view, located in \Views\Form\EditorTemplates\SectionModel.cshtml (for Sitecore 8 update 5 and earlier). On lines 18-21, the code reads:
#if (string.IsNullOrEmpty(Model.Information))
{
<p class="#Model.CssClass display-section-info">#Html.Sitecore().Field("Information", Model.InnerItem)</p>
}
The first line here should read:
#if (!string.IsNullOrEmpty(Model.Information))
Note the "!". That explains why you were seeing the markup previously, even though the parameter was not set. You need to update the code in the view in order to fix it.

Sitecore SPEAK UI configure ListControl to display icon and button

Is there a way in Sitecore SPEAK UI (7.5) to configure a ListControl (ViewMode set to DetailList) to contain a column with images, and another column containing buttons?
I've created a ListControl Parameters item beneath my PageSettings item and have added a few ColumnField items for the required columns - but cant find any other template types to add for different types of column data. I've also tried playing around with the Formatter and HTMLTemplate fields of the ColumnFields but am not sure how these are meant to be used.
Considering button means hyperlink. You can try adding the following in the HTMLTemplate:
For Image:
<img src="{{YourImageSourceField}}" ..../>
For Hyperlink:
{{YourLinkTextField}}
You can also consider reading Martina Welander Speak Series for some information on this kind of custom implementations.
I've also used the custom title property of the ListView by setting ViewMode to TileList. Then used Knockout to databind to a custom tile using standard cshtml, if this is any use?
<div class="sc-tile-default" data-bind="attr: {id: Id}">
<div style="min-height: 98px;">
<img width="112" data-bind="attr: {src: Path}" />
</div>
<div class="sc-iconList-item-title">
<span data-bind="text: Name"></span>
</div>
See this project
https://github.com/sobek1985/WallpaperManager

Using Bootstrap wysiwyg text editor in Django Form

I am using Django and Bootrap 2.32. I want to include this wysiwyg-bootrap-themed text editor: http://mindmup.github.io/bootstrap-wysiwyg/. The usage of this editor is fairly simple, including
$('#editor').wysiwyg();
in the JS-declaration will render each
<div class=editor></div>
into a beatiful wysiwyg text-editor.
Now the problem: I want to include this editor into one of my django form field. I have the single form:
class Article_Form(ModelForm):
Article_text = CharField(widget=Textarea(attrs = {'id' : 'editor'}))
class Meta:
model= Article
, whereas the Article model includes one simple CharField . Is there any chance, to get the editor work inside the Article_text form-field? With the above-mentioned widget, the created textarea cannot be controlled by the wysiwyg-editor-control buttons. Wrapping the form-template-tag like this
<div id="editor">
{{ Article_Form.Article_text }}
</div>
doesn't work either. The problem thus is that Django creates a textarea, wheras the editor would need a <div> to render correctly. Do you guys have any idea how to get this to work (without refering to django-wysiwyg).
Thanks!
I don't know enough about Django but I wrote the editor you're referring to, so here's a suggestion. Assuming the other answer on this page is correct and you can't generate a div directly, you can generate a text area using whatever Django templates you would normally do, then assign two events:
1) page onload event that would copy the textarea contents into the div, something like
$('#editor').html($('#textarea').val())
2) form onsubmit event that would reverse copy the current div contents into the textarea before it gets submitted
$('#textarea').val($('#editor').html())
Take a look at this.
Summernote is a simple WYSIWYG editor based on Twitter's Bootstrap.
django-summernote plugin allows you to embed Summernote into your Django admin page very handy.
https://github.com/lqez/django-summernote
Are you sure that this "plugin" doesn't work with textarea?
{{ Article_Form.Article_text }}
will be rendered to something like:
<textarea cols="40" id="id_Article_text" name="Article_text" rows="10"></textarea>
So there is a chance that you can initialize the wysiwyg editor like:
$('#id_Article_text').wysiwyg();
However after checking the plugin, I doubt that would be possible since it is using contenteditable="true" attribute of HTML5 and probably the plugin works with div only.
So there is no way you can make it work natively with Django form. The solution should be display other fields of your form manually, hide the one with textarea and display the editor instead:
<form action="" method="POST">
{{ Article_Form.field1 }}
{{ Article_Form.field2 }}
<div class=editor></div>
{% csrf_token %}
<input type="submit" value="Submit" id="submit-btn" />
</form>
Then you can use JS to submit your form:
$('#submit-btn').click(function (e) {
e.preventDefault();
$.ajax({
// do your magic here.
// note that you can get the content of the editor with: $('#editor').cleanHtml();
})
});
This way is hackish I agree so I don't recommend you go for it, just find other plugin then. Also please read PEP 8 carefully.
Hope it helps.
Take a look at this repo: https://github.com/rochapps/django-secure-input
I think it solves most of your problems.