简体   繁体   English

如何使用$ .post()将Jquery变量发送到我的views.py,以及如何在不刷新页面的情况下检索它们

[英]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.

相关问题 如何将变量从模板传递到views.py - How do I pass my variable from template to views.py 如何通过单击复选框而不刷新页面来发布表单? - How do I post form by clicking a checkbox without refreshing the page? 我如何将views.py中的数字列表发送到我在前面使用的chartjs文件? - how can i send a list of numbers from views.py to a chartjs file im using for the front? 如何通过javascript文件中的ajax调用在views.py中调用特定功能? - how do I call specific function in views.py through ajax call in javascript file? 如何为views.py中的对象创建属性,以便将更改的对象传递给JS代码 - How do I create property for objects in views.py in order to pass changed object to JS code 我如何使用脚本在脚本文件或views.py中运行python函数来不加载页面 - How Can I run a python function in script file or in the views.py using javascript for not loading the page 如何在views.py中使用ajax请求发送好友请求而不重新加载页面 - How to use ajax request to send friend request in views.py without reloading page Django如何使用JavaScript函数将请求发送到views.py - Django How to send request to views.py using javascript function 如何在不刷新整个页面的情况下使用jQuery自动重新加载页面的一部分? - How do I auto-reload a section of the page with jQuery without refreshing entire page? 如何在python vue js的views.py中处理GET和POST请求? - How can I able to handle both GET and POST requests in views.py in python vue js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM