1

I am working on an implementation of django-floppyforms, specifically the slider widget. However I cannot get it to display in my django web application. The native slider will work but the jQuery slider will not. The below image shows the problem.

enter image description here

The Error

> UserWarning: A {% csrf_token %} was used in a template, but the
> context did not provide the value.  This is usually caused by not
> using RequestContext.   warnings.warn("A {% csrf_token %} was used in
> a template, but the context did not provide the value.  This is
> usually caused by not using RequestContext.")

This Question, this question, and this question all suggest making sure that the django.core.context_processors.csrf context processor is used, one of the solutions is to use RequestContext from django.template

As you can see below I have tried to implement it but it keeps giving me the same error.

Any ideas?

  • Django==1.6.2
  • django-floppyforms==1.2.0

views.py

def sview(request):
   jquery_slider = Slider()
   native_slider = SlideForm()

   return render_to_response('slider_one.html', {
                                          'jquery_slider': jquery_slider,   
                                          'native_slider': native_slider,             
                                          }, context_instance=RequestContext(request))

slider_one.html

{# slider.html #}
{% include "floppyforms/input.html" %}
<div id="{{ attrs.id }}-slider"></div>

<html> 
    <body>
        <h1>This is your slider_one.html template</h1>

<script type="text/javascript" src="{{ STATIC_URL }}/js/jquery.min.js"></script> 
<script type="text/javascript" src="{{ STATIC_URL }}/js/jquery-ui.min.js"></script> 
<script type="text/javascript" src="{{ STATIC_URL }}/css/jquery-ui.min.css"></script> 


<form action="" method="post">
    {% csrf_token %}

        {{ jquery_slider }}
        {{ native_slider }}

    <input type="submit" value="Submit" />               
</form>

<script type="text/javascript">
  $(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

import floppyforms as forms    

class Slider(forms.RangeInput):
    min = 5
    max = 20
    step = 5
    template_name = 'slider_one.html'

    class Media:
        js = (
            '/static/js/jquery.min.js',
            '/static/js/jquery-ui.min.js',
        )
        css = {
            'all': (
                '/static/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

EDIT:

I also have the below enabled in the MIDDLEWARE_CLASSES of my settings.py file

django.middleware.csrf.CsrfViewMiddleware'
Community
  • 1
  • 1
Deepend
  • 4,057
  • 17
  • 60
  • 101

2 Answers2

1

I had the same problem. As @professorDante said, you need:

return render(request, "slider_one.html", local())

The key is that the request object must be passed to the template in order to generate csrf.

max
  • 9,708
  • 15
  • 89
  • 144
0

Try using the render() shortcut - you're wanting to use a RequestContext, and that method uses one by default. If it works, you know it's your implementation of return_to_render.

from django.shortcuts import render 

return render(request, "slider_one.html",{'jquery_slider':jquery_slider,'native_slider':native_slider})
professorDante
  • 2,290
  • 16
  • 26
  • Hi, I should have mentioned that I have already tried that before the `render_to_response` method. Unfortunately it does not work – Deepend Jul 09 '14 at 16:02