I am trying to implement the Slider from django-floppyforms. However my page only displays a single empty text box when I use the examples given. When I render it using {{form.as_p}}
I can only get the native slider to work but I I would really like the JS functionality.
I have a copy of jquery-1.11.1.min.js
downloaded from http://jquery.com/download/ Its filepath is floppyform_test/static/js/jquery-1.11.1.min.js
`floppy_project/src/floppyform_test/static/js
I think it might be a problem with my jQuery filepath (I have tried many variations) or my views.py
I haven't really used JavaScript before but here is what I have tried.
views.py
def sview(request):
jquery_slider = Slider()
native_slider = SlideForm()
return render(request, 'slider.html', {
'jquery_slider': jquery_slider,
'native_slider': native_slider,
})
slider.html (adapted from original)
{# slider.html #}
{% include "floppyforms/input.html" %}
<div id="{{ attrs.id }}-slider"></div>
<script type="text/javascript" src="{{ STATIC_URL }} /static/js/jquery-1.11.1.min.js">
$(document).ready(function() {
var type = $('<input type="range" />').attr('type');
if (type == 'text') { // No HTML5 support
$('#{{ attrs.id }}').attr("readonly", true);
$('#{{ attrs.id }}-slider').slider({
{% if value %}value: {{ value }},{% endif %}
min: {{ attrs.min }},
max: {{ attrs.max }},
step: {{ attrs.step }},
slide: function(event, ui) {
$('#{{ attrs.id }}').val(ui.value);
}
});
}
});
</script>
forms.py (copied from original)
import floppyforms as forms
class Slider(forms.RangeInput):
min = 5
max = 20
step = 5
template_name = 'slider.html'
class Media:
js = (
'js/jquery.min.js',
'js/jquery-ui.min.js',
)
css = {
'all': (
'css/jquery-ui.css',
)
}
class SlideForm(forms.Form):
num = forms.IntegerField(widget=Slider)
def clean_num(self):
num = self.cleaned_data['num']
if not 5 <= num <= 20:
raise forms.ValidationError("Enter a value between 5 and 20")
if not num % 5 == 0:
raise forms.ValidationError("Enter a multiple of 5")
return num
Any help is greatly appriciated
Three things:
Can you find the jquery file at the {{static_url}}/static.... path? If you've set your static_url to be /static/, you don't need the /static added again.
You've declared more jquery in your Slider widget, but its a different path and version to your declaration in your template, so you'll get a clash.
Don't declare your jquery file as you have done - put it in the head of the doc, then wite your ad-hoc script using the $(document).ready(function(){}) syntax