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
.
我希望将html文本字段名称=“slider_value”的值返回到Django类sessionwizard - dview中,特别是get_context_data方法中。通过浏览这个网站和其他网站,我明白我需要使用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.
从这个答案中,我找到了slider_value = self.request。POST['slider_value'],但它只返回一个"None"值。我猜这是因为当时的文本框没有任何价值。
My problem is that I only want the value of slider_value
stored when the user clicks submit on that form page.
我的问题是,我只想要用户单击表单页面上的submit时存储的slider_value的值。
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?
谁能告诉我,在用户单击submit按钮后,如何将html文本字段名称="slider_value"的值输入到get_context_data方法中?
Below is my code so far
下面是我目前的代码
views.py
views.py
This is a shortened version of my get_context_data method
这是我的get_context_data方法的缩短版本
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
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)
这是我的html表单,我试图获取文本字段名'slider_value'的值。它对应于jQuery滑动条(值-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
slider_two.js
This is my jQuery slider. It updates the above text field, slider_value
这是jQuery滑动条。它更新上面的文本字段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!
一如既往,谢谢!
2 个解决方案
#1
0
To get the value you need a request, you can use self.request. Just initialize a form and get it from there.
要获取需要的值,可以使用self.request。只需初始化一个表单,然后从那里获取它。
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
#2
1
First, in your click
listener, where you tried to ge slider_value you are referencing the wrong id, it should be $("#hidden1")
;
首先,在您的单击侦听器中,您试图使用slider_value引用错误的id,它应该是$(“#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'.
其次,在您的post Ajax调用中,您将slider_value的值作为参数名隐藏,在您的视图中,您试图以'slider_value'的名称获取它,它应该是'hidden'。或者可以在Ajax调用中更改为'slider _value'。
#1
0
To get the value you need a request, you can use self.request. Just initialize a form and get it from there.
要获取需要的值,可以使用self.request。只需初始化一个表单,然后从那里获取它。
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
#2
1
First, in your click
listener, where you tried to ge slider_value you are referencing the wrong id, it should be $("#hidden1")
;
首先,在您的单击侦听器中,您试图使用slider_value引用错误的id,它应该是$(“#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'.
其次,在您的post Ajax调用中,您将slider_value的值作为参数名隐藏,在您的视图中,您试图以'slider_value'的名称获取它,它应该是'hidden'。或者可以在Ajax调用中更改为'slider _value'。