Muat Ulang Fungsi Tampilan Django dengan Mengatur Ulang Data Konteks
//parent_template.html
{% load static %}
<script src="{% static "jquery.js" %}"></script>
{% block first %}
<div class="row">
{% for x in my_list %}
<button class="some-filter" id="{{x}}"></button>
{% endfor %}
</div>
{% endblock first %}
{% block second %}
<div class="row" id="some_id"></div>
{% endblock second %}
<script>
$(".some-filter").on({
click: e=> {
var name = $( e.target )[0].id;
$.ajax({
url: '/ajax/info_getter/',
data: {
'name': name,
},
dataType: 'html',
success: function (data) {
if (data) {
$("#some_id").html(data);
$(".some-filter").removeClass('selected');
$( e.target ).addClass('selected');
}
}
});
}
})
</script>
// child_template.html
// my big ass template containing lots of image files
{% load static %}
...other stuff
// urls.py
from django.urls import path
from . import views
urlpatterns = [
...
path('ajax/info_getter/', views.info_getter, name='info_getter'),
...
]
// views.py
from django.shortcuts import render
...
def info_getter(request):
name = request.GET.get('name', None)
if name:
context["name"] = name
response = render(request, "child_template.html", context=context)
return response
Jittery Jackal