如何使用AJAX更新Django中的查询?还是可能的?

时间:2023-01-14 19:36:50

For example I have this in my template:

例如,我在我的模板中有这个:

{% for pins in data %}
    <option type="text" value="{{pins.clandpin}}">{{pins.clandpin}}</option>
{% endfor %}

And in my view.py:

在我的view.py中:

def section_landpins(request):
    if request.method == "GET":
         section_id = request.GET['sectionid']
         m = ButuanMaps.objects.filter(ssectionid=section_id)
         data = serializers.serialize("json", m)
         return HttpResponse(data, content_type='application/json')

How do I use the response from my view.py in my template using AJAX?

如何使用AJAX在我的模板中使用view.py中的响应?

    $(document).ready(function(){
    $("#formsection").change(function() {
        $('#forminput').empty();
        $.ajax({
            url : "/sectionpins",
            type : "GET",
            dataType: "json",
            data : {
                'sectionid' : $(this).val(),
                'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
            },
            success : function(data){
               ......
            },
            error: function (){
                alert('error');
            }
        });
    return false;
    });
 });

2 个解决方案

#1


0  

If you render the from to a string like this, you can than put the HTML directly into the form,

如果将from渲染为这样的字符串,则可以将HTML直接放入表单中,

def section_landpins(request):
    if request.method == "GET":
        section_id = request.GET['sectionid']
        pins = ButuanMaps.objects.filter(ssectionid=section_id)
        html = render_to_string('pathToTemplate', {'pins ': pins })
        return HttpResponse(html)

Take the rendered HTML and put it into whereever you see fit using selectors.

获取渲染的HTML并使用选择器将其放入您认为合适的位置。

$(document).ready(function(){
    $("#formsection").change(function() {
        $('#forminput').empty();
        $.ajax({
            url : "/sectionpins",
            type : "GET",
            dataType: "json",
            data : {
                'sectionid' : $(this).val(),
                'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
            },
            success : function(data){
                $('form').html(data);
            },
            error: function (xhr, textStatus, thrownError){
                alert("xhr status: " + xhr.statusText);
            }
        });
        return false;
    });
});

#2


0  

you can test if a request use ajax

您可以测试请求是否使用ajax

def section_landpins(request):
    if request.method == "GET":
         section_id = request.GET['sectionid']
         m = ButuanMaps.objects.filter(ssectionid=section_id)
         data = serializers.serialize("json", m)
         if request.is_ajax():
               return HttpResponse(data, content_type='application/json')
         else:
               rendre_to_response(........)

#1


0  

If you render the from to a string like this, you can than put the HTML directly into the form,

如果将from渲染为这样的字符串,则可以将HTML直接放入表单中,

def section_landpins(request):
    if request.method == "GET":
        section_id = request.GET['sectionid']
        pins = ButuanMaps.objects.filter(ssectionid=section_id)
        html = render_to_string('pathToTemplate', {'pins ': pins })
        return HttpResponse(html)

Take the rendered HTML and put it into whereever you see fit using selectors.

获取渲染的HTML并使用选择器将其放入您认为合适的位置。

$(document).ready(function(){
    $("#formsection").change(function() {
        $('#forminput').empty();
        $.ajax({
            url : "/sectionpins",
            type : "GET",
            dataType: "json",
            data : {
                'sectionid' : $(this).val(),
                'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
            },
            success : function(data){
                $('form').html(data);
            },
            error: function (xhr, textStatus, thrownError){
                alert("xhr status: " + xhr.statusText);
            }
        });
        return false;
    });
});

#2


0  

you can test if a request use ajax

您可以测试请求是否使用ajax

def section_landpins(request):
    if request.method == "GET":
         section_id = request.GET['sectionid']
         m = ButuanMaps.objects.filter(ssectionid=section_id)
         data = serializers.serialize("json", m)
         if request.is_ajax():
               return HttpResponse(data, content_type='application/json')
         else:
               rendre_to_response(........)