I found a partial solution on this page . In the base template, I created a button and a form:
<div class='feedback-button'><a class="btn btn-info" href="#feedbackModal" data-toggle="modal" title="Leave feedback" target="_blank"><i class="icon-comment icon-white"></i> Leave feedback</a></div> {% include "_feedback_form.html" with feedback_form=feedback_form %}
Then I created two forms of feedback
<div class="modal hide" id="feedbackModal" tabindex="-1" role="dialog" aria-labelledby="feedbackModalLabel" aria-hidden="true"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button> <h3 id="feedbackModalLabel">Contact Form</h3> </div> {% include "_feedback_form_two.html" with feedback_form=feedback_form %} </div>
and
{% load crispy_forms_tags%}
<form action="{% url feedback %}" method="post" id="id-Crispy_ContactForm" class="form ajax" data-replace="#id-Crispy_ContactForm"> <div class="modal-body"> {% crispy feedback_form %} </div> <div class="modal-footer"> <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> <input type="submit" name="submit_feedback" value="Submit" class="btn btn-primary" id="submit-id-submit_feedback" /> </div> </form>
I broke the feedback forms into two, because the bootstrap-ajax.js file, which I use from the link above, replaces the html from one template. If I use a combined feedback form, it will have class = "modal hiding". I need it to have class = "modal", so if the form is updated with errors, the modality does not disappear.
In my opinion, I have
@login_required def feedback_ajax(request): feedback_form = Crispy_ContactForm(request.POST) dismiss_modal = False if feedback_form.is_valid(): message = feedback_form.save() feedback_form = Crispy_ContactForm() dismiss_modal = True data = { "html": render_to_string("_feedback_form_two.html", { "feedback_form": feedback_form }, context_instance=RequestContext(request)), "dismiss_modal": dismiss_modal } return HttpResponse(json.dumps(data), mimetype="application/json")
And then in the bootstrap-ajax.js file (again from the link above), I made a few changes. In the processData function, I defined:
var $el_parent = $el.parent();
and I added
if (data.dismiss_modal) { var msg = '<div class="alert alert-success" id="' + $(replace_selector).attr('id') + '">Feedback Submitted</div>' $(replace_selector).replaceWith(msg); $el_parent.modal('hide'); $(replace_selector).replaceWith(data.html); }
This is not yet fully functional, because the success message immediately disappears with the modal. I want the modal to display the message and disappear after 3 seconds. I haven’t figured it out yet, but now it works quite well.
I am still doing music, but that concerns most of my questions:
It sends data using AJAX and returns error checking if necessary. The form is well displayed in modal mode.
I have a few remaining issues. I need to figure out a way to suppress a legend in a crispy shape, and I need to find a way to display a modal crispy shape and not interfere with another crispy shape that appears elsewhere on the site.