[英]How do I send Jquery variable to my views.py using $.post(), and how do i retrieve them without refreshing the page
What I'm trying to do is a step form inside a modal. 我想做的是模态内部的一个步骤表格。 The contents for my step 1 are treatments or services looped from my database.
我的步骤1的内容是从数据库中循环的处理或服务。 And when I click one of these (".alert")(I used BS alerts as a button), the treatments id will be stored inside a hidden input in the next step's field set.
当我单击其中的一个(“ .alert”)(我将BS警报用作按钮)时,处理ID将存储在下一步字段设置的隐藏输入中。 What I want to happen is to send that id back to my views.py and retrieve it so I could get the information about the treatment's duration.
我想要发生的是将该ID发送回我的views.py并进行检索,这样我就可以获得有关治疗时间的信息。
The function in my js where the magic should happen 我的js中应该发生魔术的函数
$(".alert").click(function(){
service_id = $(this).find("input").val();
current_fs = $(this).parents("fieldset");
next_fs = $(this).parents("fieldset").next();
$("#nav li").eq($("fieldset").index(next_fs)).addClass("active");
$("#nav li").eq($("fieldset").index(current_fs)).removeClass("active");
current_fs.hide();
next_fs.show();
next_fs.find("div").find("input").val(service_id);
$.post("{% url 'calendarium:data' %}",{id: service_id});
});
my views.py 我的views.py
@csrf_exempt
def time(request):
data = request.POST.get('id','')
treatment = get_object_or_404(Services, pk = data)
return render(request,'calendarium/calendar_msform.html',
{'treatment':treatment})
my calendar_msform.html 我的calendar_msform.html
<fieldset id="#step-2" style="display: none">
<div style="display:block;
border-style:solid;
border-color:#aa9977;
border-radius: 5px;
box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
padding: 5px">
<input type="hidden" value="">
{% if treatment.duration == 30 %}
<div class="alert alert-default" style="padding: 2px">
10:00 - 10:30 am
<label style="float: right; margin-right: 10px">
<small>{{ s.duration }} mins</small></label>
</div>
{% else %}
{% endif %}
</div>
<input id="backStep1" type="button" name="previous" class="previous
action-button-previous" value="Previous"/>
<input id="toStep3" type="button" name="next" class="next action-button"
value="Next"/>
</fieldset>
I've successfully sent my data back to my views, but I cant access it on my html file. 我已成功将数据发送回视图,但无法在html文件中访问它。 I'm a newbie in jquery and django so i really need help.
我是jQuery和Django的新手,所以我真的需要帮助。 Thank you.
谢谢。
You need to use the response of your $.post and update your page accordingly. 您需要使用$ .post的响应并相应地更新页面。 The page won't automatically update with the view's returned HTML, you have to put it somewhere with javascript.
该页面不会使用视图返回的HTML自动更新,您必须使用javascript将其放置在某处。 For example,
例如,
$.post("{% url 'calendarium:data' %}",{id: service_id}, function(response){
$('#form-div').html(response);
});
Will put the rendered html into an arbitrary div with the id 'form-div'. 将呈现的html放入ID为“ form-div”的任意div中。 Response is the returned output from the Django view.
响应是Django视图返回的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.