如何在django中重新加载一段网页?

时间:2019-03-04 07:46:31

标签: ajax django django-templates

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

<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})

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

1 个答案:

答案 0 :(得分:0)

我发现我需要做.html(以从ajax返回的html作为参数)而不是.load。 我希望这对其他人有帮助。

相关问题