如何在django中重新载入网页的一部分? [英] how to reload a segment of webpage in django?

查看:44
本文介绍了如何在django中重新载入网页的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django中,我试图通过加载html文件来制作可更改网页某一部分的按钮.该html文件显示了地图,统计信息和其他内容,因此它们必须分开.我的方法是使用ajax调用,然后在视图中返回html文件.

In django, I am trying to make buttons that change one segment of the webpage by loading html file. The html file shows map and statistics and stuff so they have to be separate. My approach was to use ajax call and in the view, return the html file.

<script type="text/javascript" >
function mapcall (office, year){ 
        console.log(office, year, "clicked")
    $.ajax ({
        method : "GET", 
        url: '/map/', // % url "map" % end point 
        data: {
            office_called: office,
            year_called: year,
        },
        success: function (map){
            console.log("map is here ", map)
            $("#maparea").load($( "{% static 'my_html_return' %}"
            ))},
        error: function (error_data){
        alert("data error, sorry")
        }
    })
}
</script>
<div id="maparea">
    {% if map_return %}
        {% with map_template=map_return|stringformat:"s"|add:".html" %}
            {% include "mypath/"|add:map_template %}
        {% endwith %}
    {% endif %}
</div>

这是我的观点.py

def get_map (request, *args, **kwargs):
year = request.GET.get("year_called")          
office = request.GET.get("office_called") 

map_return = "map"+str(year)+office

return render(request, "mypath/home.html", 
{"title": "Home", "map_return":map_return})

我不知道该如何做.任何建议表示赞赏.

I don't know how to make this work. Any suggestion is appreciated.

推荐答案

在我的案例中,我发现我需要执行.html(以从ajax返回的html作为参数)而不是.load.我希望这对其他人有帮助.

I figured out that I needed to do .html(take html return from ajax as an argument) instead of .load in my case. I hope this helps other people.

这篇关于如何在django中重新载入网页的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆