how to reload a segment of webpage in django?

ⅰ亾dé卋堺 提交于 2019-12-25 00:22:22

问题


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>

This is my view.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.


回答1:


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.



来源:https://stackoverflow.com/questions/54978693/how-to-reload-a-segment-of-webpage-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!