Reputation: 4155
I want to get the value of a html text field name="slider_value"
back into a Django class SessionWizardView, specifically into the get_context_data
method. From reading around on this site and others I understand I need to use self.request
.
From this answer I found slider_value = self.request.POST['slider_value']
but it only returns a "None" value. I am guessing this is because there is no value in the text field at the time.
My problem is that I only want the value of slider_value
stored when the user clicks submit on that form page.
Can anyone tell me how can I get the value of the html text field name="slider_value"
into my get_context_data method after the user has clicked the submit button?
Below is my code so far
views.py
This is a shortened version of my get_context_data method
class SurveyWizardOne(SessionWizardView):
def get_context_data(self, form, **kwargs):
context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)
if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16']:
step = int(self.steps.current)
if step in (5, 6, 7):
image = random.choice(PATH_ONE_IMAGES)
images.insert(step - 5, image)
PATH_ONE_IMAGES.remove(image)
context['display_image'] = image
if step == 5:
print 'you are on step 5'
slider_value = self.request.POST['slider_value']
print 'This is the slide value', slider_value
....
....
return context
html
This is my html form, I am trying to get the value of the text field names 'slider_value'. It corresponds to a jQuery slider (value -100 - +100)
<form action="" method="post">{% csrf_token %}
<img src="{% static "survey/images/pathone/" %}{{display_image}}"/>
<div class="slider" id="one"></div>
<div id="slider-result"></div>
<input type="text" name="slider_value" id="hidden1"/>
<script src="{% static "survey/js/slider_two.js" %}"></script>
<input type="submit" value="{% trans "submit" %}"/>
</form>
slider_two.js
This is my jQuery slider. It updates the above text field, slider_value
$('#submit').click(function() {
var username = $('#hidden').val();
if (username == "") username = 0;
$.post('comment.php', {
hidden: username
}, function(return_data) {
alert(return_data);
});
});
$(".slider").slider({
animate: true,
range: "min",
value: 0,
min: -100,
max: +100,
step: 1,
slide: function(event, ui) {
$("#slider-result").html(ui.value);
if($(this).attr("id") == "one")
$("#hidden1").val(ui.value);
}
});
As always, thanks!
Upvotes: 2
Views: 3245
Reputation: 2089
To get the value you need a request, you can use self.request. Just initialize a form and get it from there.
As a reference you could use this example in the docs.
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(PublisherDetail, self).get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['book_list'] = Book.objects.all()
form = myForm(request.POST or None)
return context
Upvotes: 0
Reputation: 126
First, in your click
listener, where you tried to ge slider_value you are referencing the wrong id, it should be $("#hidden1")
;
Second, in your post
Ajax call, you sent slider_value
's value as parameter name hidden
, and in your view, you tried to get it by name 'slider_value', it should be 'hidden'. Or you can change it in your Ajax call to 'slider _value'.
Upvotes: 1