could someone help me find a way to make my ajax request work?
I found some of the same questions on this site but didn’t solve them.
I want to Ajax the process to show modal
I’m using the latest Django version. I have never used ajax, so I don’t really know what I’m doing. Please help
What should I specify for the url?
Url to display Ajax?
Url to process Ajax?
app.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', include('test.urls')),
]
test/urls.py
urlpatterns = [
path('rex/', IndexView.as_view(), name='rex_index'),
]
test/templates/test/rex.html
{% extends 'main/layout.html' %}
{% block content %}
{% load crispy_forms_tags %}
<!--modal-->
<script>
$(function(){
$('#edit_foo').on('click', function () {
console.log("test")
$.ajax({
type: 'POST',
url: '/rex_index/',
data: {
html: '<form id="form_foo"><textarea></textarea><input type="submit"></input></form>',
delay: 1,
},
success: function (data, textStatus, jqXHR) {
$('#foo_modal').find('.modal-body').html(data);
$('#foo_modal').modal('show');
},
});
});
});
// Handle submit. Here we return an error regardless of the
// input given:
$("#foo_modal").on('submit', '#form_foo', function (e) {
$.ajax({
type: 'POST',
url: '/rex_index/',
data: {
html: '<form id="form_foo_2"><span class="error">You must write something silly here:</span><textarea></textarea><input type="submit"></input></form>',
delay: 0,
},
success: function (data, textStatus, jqXHR) {
$('#foo_modal').find('.modal-body').html(data);
},
});
e.preventDefault();
return false;
});
// Handle second submit. Here we close the modal.
// In a real application you can use the status to determine
// which action to take (success or redisplay the form).
$("#foo_modal").on('submit', '#form_foo_2', function (e) {
alert('Thanks for writing something silly. Closing modal');
$('#foo_modal').modal('hide');
e.preventDefault();
return false;
});
</script>
<button type='button' id='edit_foo' class='buttons'>push me</button>
<div class="modal fade" id="foo_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">Server Side Modal Popup Demo</div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
</div>
</div>
{% endblock %}
views.py
***There is no description related to modal in view**
class IndexView(LoginRequiredMixin,FilterView):
model = Order
filterset_class = OrderFilter
template_name = 'test/rex.html'
queryset = Order.objects.all()
strict = False
paginate_by = 10
def get(self, request, **kwargs):
if request.GET:
print("実行")
request.session['query'] = request.GET
print("query",request.session['query'])
else:
request.GET = request.GET.copy()
if 'query' in request.session.keys():
for key in request.session['query'].keys():
request.GET[key] = request.session['query'][key]
return super().get(request, **kwargs)