1👍
✅
You should separate your template with form into two:
form.html
{# usual header and containers to work on separate page correctly #}
...
{% includes "your_app/includes/ajax_form.html" %}
....
ajax_form.html
{# just your form here or container that will be sent via ajax only #}
<form ...>
</form>
Also inside your template where you want to have popup you should again include your ajax_form.html
template. Don’t forget to set correct action
in form to send requests to your FormView
.
Then you have to create your view with overriden get_template_names
method:
class YourFormView(FormView):
...
def get_template_names(self):
if self.request.is_ajax():
return ['your_app/includes/ajax_form.html']
return ['your_app/form.html']
Usual request – complete template is returned. Ajax request – only form template is returned.
Source:stackexchange.com