简体   繁体   English

点击每个链接的django加载模式

[英]django load modal on click for each link

In my project I have many Buildings , and in the buildings.html I display each Building using a single image ( big_image ). 在我的项目中,我有很多Buildings ,在buildings.html我使用单个图像( big_image )显示每个Building What I want to do is load a modal every time the user clicks in a Building , this modal allows the user to see additional images of the building. 我想要做的是每次用户点击Building时加载模态,此模态允许用户查看建筑物的其他图像。

This would be very easy if I loaded each Building 's modal when the user entered the page. 如果我在用户进入页面时加载每个Building的模态,这将非常容易。 But if I did that the page would take ages to load (because there are many Buildings ), so my idea is to use jQuery's .innerHTML to append the modal html to the page when the user clicks on a Building , but when I do that the only code appended is 但是,如果我这样做,页面将花费很长时间来加载(因为有很多Buildings ),所以我的想法是使用jQuery的.innerHTML在用户点击Building时将模态html附加到页面,但是当我这样做时附加的唯一代码是

`{% for building in buildings %}{% if building.pk == 1(or whatever is the pk) %}{% endif %}{% endfor %}`

There might be an easier way to achieve that that I dont know 可能有一种更简单的方法来达到我不知道的目的

Here's my code: 这是我的代码:

buildings.js buildings.js

function loadModal(objectPk){
    document.getElementById("loadModal").innerHTML =

"{% for building in buildings %}{% if building.pk == "+objectPk+" %}
<div class="modal fade mymodal in " id="myModal{{ building.pk }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
 aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">{{ building.name }}</h4>
        </div>
        <div class="modal-body">
            <p>{{ building.short_description }}</p>
            {% for i in building.images.all %}
            <img src="{{ i.image.url }}" class="img-responsive">
            {% endfor %}
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
        </div>
    </div>
</div>
</div>{% endif %}{% endfor %}";
}

All of the above is in a single line. 所有上述内容都在一行中。

buildings.html buildings.html

<a data-toggle="modal" data-target="#myModal{{ building.pk }}" href="" onClick="JavaScript:loadModal({{ building.pk }});"></a>
<div id="loadModal"></div>

views.py just in case views.py以防万一

class BuildingsModalView(generic.ListView):
    template_name = 'info/buildings.html'

    context_object_name = 'construction_list'

    queryset = Residence.objects.all() #ignore this

    def get_context_data(self, **kwargs):
        context = super(BuildingsModalView, self).get_context_data(**kwargs)
        context['building'] = Buildings.objects.all()
        # And so on for more models
        return context

You can use a little bit of javascript to change the values in the modal's html. 您可以使用一些javascript来更改模态的html中的值。 In this example, I create a link for each building, with data attributes for the img, description, and name. 在此示例中,我为每个建筑创建了一个链接,其中包含img,description和name的数据属性。

Then, I add a click listener to the class of the building link. 然后,我将一个点击监听器添加到建筑物链接的类中。 When clicked, it accesses all of those data values and pops them into the correct spot of the modal. 单击时,它将访问所有这些数据值并将它们弹出到模态的正确位置。

<!-- Create a unique link for every building -->
{% for building in buildings %}
<a href="#"
    class="building-link"
    data-toggle="modal"
    data-target="#building-modal"
    data-img="{{ building.img.url }}"
    data-description="{{ building.short_description }}"
    data-name="{{ building.name }}">
    {{ building.name }}
</a>
{% endfor %}

<!-- This is our blank modal, with ids for the values to be injected -->
<div class="modal fade mymodal in " id="building-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
 aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="building-name"></h4>
            </div>
            <div class="modal-body">
                <p id="building-description"></p>
                <img id="building-img" src=""/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
            </div>
        </div>
    </div>
</div>

<!-- When a link is clicked, grab all the data values and pop them inro the modal -->
<script>
$('.building-link').click(function(){
    $('#building-name').html($(this).data('name'));
    $('#building-description').html($(this).data('description'));
    $('#building-img').src($(this).data('img'));
});
</script>

Then only thing this doesn't do is do all of the building images. 然后,唯一不做的就是完成所有建筑图像。 I would recommend just loading one to begin with and then setting up another endpoint to get all of the building images once one is selected. 我建议只需加载一个开始,然后设置另一个端点,以便在选择一个建筑图像后获取所有建筑图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM