django - How to save the value of a jQuery sliderbar into a Person model? -
i trying save value of jquery element added surveywizardform
class person(models.model):
, therefore database.
all of other elements in form created normal way, creating necessary fields , widgets in forms.py , data each question in models.py. result automatically work , correct data getting saved.
however have customized surveywizardform
on pages allow user submit rating image via jquery slider bar.
my issue cannot seem store value in person model.
question: how store value of slider bar slider_value
in person model?
all attempts far have created new entry in model/db not store value have in form/views.py. fear not linking 2 pieces correctly.
any appreciated. thanks
my code far
slider_two.js
this updates hidden form field hidden1
value of jquery slider bar
if($(this).attr("id") == "one") $("#hidden1").val((ui.value > 0 ? '+' : '') + ui.value);
wizard_form.html
value="0"
gets updated when user moves slider created in above slider_two.js
(not shown)
<input type="hidden" name="slider_value" value="0" id="hidden1"/> <script src="{% static "survey/js/slider_two.js" %}"></script>
views.py
i can read value views.py using
slider_value = self.request.post.get('slider_value') if slider_value not none: instruction_task_values.insert(0, slider_value) logger.debug('\n\n\nthis instruction_task_values in 1 %s', instruction_task_values)
this works correct value printed each time log
models.py
this meant create field in db store value of slider bar. other entries create work , saved correctly.
class person(models.model): slider_value = models.integerfield(null=true, blank=true, max_length=1000)
forms.py
this believe issue is.
the below attempt "connect" existing slider_value
in form , add person model create new fiel in form.
class surveyformit1(forms.modelform): class meta: model = person fields = ['slider_value'] widgets = {'slider_value' : forms.hiddeninput}
you should consider revising code work forms properly.
for example, in views.py:
def my_view(request): # if post request need process form data if request.method == 'post': # create form instance , populate data request: form = surveyformit1(request.post) # check whether it's valid: if form.is_valid(): # process data in form.cleaned_data required slider_value = form.cleaned_data['slider_value'] if slider_value not none: instruction_task_values.insert(0, slider_value) logger.debug('\n\n\nthis instruction_task_values in 1 %s', instruction_task_values) # redirect new url: return httpresponseredirect('/thanks/') # if (or other method) we'll create blank form else: form = surveyformit1() return render(request, 'wizard_form.html', {'form': form})
in wizard_form.html:
{{ form.as_p }} <script src="{% static "survey/js/slider_two.js" %}"></script>
{{ form.as_p }}
give like
<input type="hidden" name="slider_value" value="" id="id_slider_value"/>
Comments
Post a Comment