简体   繁体   English

如何在Django视图中发布和捕获jQuery ui-slider输出值?

[英]How to POST and capture a jQuery ui-slider outputted value in a Django view?

I am trying to POST the value from a jQuery UI slider that appears on an internal page of a survey form to a Django SessionWizardView. 我正在尝试将调查表的内部页面上显示的jQuery UI滑块中的值发布到Django SessionWizardView。 I am pretty new to JavaScript and jQuery but have some experience with Python 2.7.3 / Django 1.6.2. 我对JavaScript和jQuery很陌生,但是对Python 2.7.3 / Django 1.6.2有一定的经验。

Question: How do I 'send' and 'capture' the data sent from a jQuery ui-slider via POST in a Django view? 问题:如何在Django视图中“发送”和“捕获”通过POST从jQuery ui-slider发送的数据?

Aditional questions/info 附加问题/信息

wizard_form.html wizard_form.html

I have a jQuery-UI Slider adopted from a tutorial by Thoriq Firdaus . 我有一个Thoriq Firdaus的教程采用的jQuery-UI Slider。 This appears on an internal page within my SessionWizardView. 这显示在我的SessionWizardView的内部页面上。

<form action="" method="post">{% csrf_token %}
            <table>   
                {{ wizard.management_form }}
                    {% if wizard.form.forms %}
                        {{ wizard.form.management_form }}
                        {% for form in wizard.form.forms %}
                            {{ form }}
                        {% endfor %}
                    {% else %}
                        {{ wizard.form }}
                {% endif %}   
            </table>

        {% load staticfiles %}  
            {% if 'surveyone' in request.path %}                        
                     {% if wizard.steps.current in steps %}     
                            <img src="{% static "survey/images/pathone/" %}{{display_image}}" height="600" width="500" style="border:1px solid black;" align="middle"/>                                                                     

                            <section>                               
                                <span class="tooltip"></span>   
                                <div id="slider"></div>  
                                <span class="volume"></span>  
                            </section>  

/my_project/src/survey/static/survey/js/slider.js /my_project/src/survey/static/survey/js/slider.js

This is the relevant part of my slider.js code. 这是我的slider.js代码的相关部分。 I am trying to use jQuery.post to post the value to my Django view. 我正在尝试使用jQuery.postvalue到我的Django视图中。

slide: function (event, ui) { 
    var value = slider.slider('value'),
        volume = $('.volume');                
        console.log(value)                
        $.post("/surveyone/", {value : value, csrfmiddlewaretoken : '{{csrf_token}}' } );
        },

/my_project/src/survey/views.py /my_project/src/survey/views.py

This is my SessionWizardView which contains get_context_data and the done methods, both of which work fine. 这是我的SessionWizardView,其中包含get_context_datadone方法,两者都可以正常工作。 My problem is that I'm not really sure how to 'receive' the posted data. 我的问题是我不确定如何“接收”已发布的数据。

I have read through the Form Wizard documentation but nothing stands out, the closest thing is the render(self, form=None, **kwargs): method, but that says it should be used "after the GET or POST request has been handled" 我已经阅读了“表单向导”文档,但没有任何内容引人注目,最接近的是render(self, form=None, **kwargs):方法,但是它说“应该在处理了GET或POST请求之后使用” “

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

            elif step == 8:
                context['first_image'] = images[0]
                context['second_image'] = images[1]
                context['third_image'] = images[2]             

            elif step in (9, 10, 11):
                image = random.choice(PATH_ONE_IMAGES)   
                images.insert(step - 6, image)     
                PATH_ONE_IMAGES.remove(image)
                context['display_image'] = image

            elif step == 12:
                context['fourth_image'] = images[3]
                context['fifth_image'] = images[4]
                context['sixth_image'] = images[5]


            steps = ['5','6','7','9','10','11']              
            dv_steps = ['8','12']          

            context.update({'steps': steps,
                            'dv_steps': dv_steps
                             })


    def done(self, form_list, **kwargs): 
        return render(self.request, 'Return_to_AMT.html', {
            'form_data': [form.cleaned_data for form in form_list],            
        })  

So in essence, I'm not really sure what to do/try next. 因此,从本质上讲,我不太确定下一步该做什么/尝试一下。 Could anyone provide any hints or tips? 谁能提供任何提示或技巧?

Since the data is to be picked in SessionWizardView i would suggest to have a hidden field slider_value in a form. 由于数据将在SessionWizardView中选取,因此我建议在表单中包含一个隐藏字段slider_value。 Now in the frontend this field is not visible. 现在在前端,此字段不可见。

The slider function will update the value in this hidden field which can be posted by posting the form. 滑块功能将更新此隐藏字段中的值,可以通过发布表单来发布该值。 The value can then be accessed from the form object in session wizrd view. 然后可以从会话向导视图中的表单对象访问该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM