在Ajax和Django中实现模态窗口的方法有很多种,下面是一种常见的解决方案,包含了代码示例:
$(document).ready(function() {
$('#myModalBtn').click(function() {
$.ajax({
url: '/get_modal_content/',
type: 'GET',
success: function(data) {
$('#myModal').html(data);
$('#myModal').modal('show');
}
});
});
});
from django.shortcuts import render
def get_modal_content(request):
if request.is_ajax():
# 处理Ajax请求
return render(request, 'modal_content.html')
from django.urls import path
from . import views
urlpatterns = [
path('get_modal_content/', views.get_modal_content, name='get_modal_content'),
# 其他URL配置
]
通过以上步骤,你可以在Ajax和Django中实现一个简单的模态窗口,并在点击按钮时显示该模态窗口。你可以根据自己的需求,自定义模态窗口的内容和样式。