在Bootstrap Modal中使用django crispy表单的AJAX反馈表单

Ed.*_*Ed. 4 django ajax modal-dialog twitter-bootstrap django-crispy-forms

这个问题有很多可动的部分,但是如果您对任何问题有任何了解,将不胜感激。

我想构建一个可以像人们期望的那样的反馈表。当用户单击页面右下方的“反馈”按钮时,它将启动引导程序模式。模态具有django酥脆形式,可提交或返回按下“提交”按钮时无效的字段。

首先,我有我的反馈按钮:

{% load crispy_forms_tags %}

.feedback-button {
    position: fixed;
    bottom: 0;
    right: 30px;
}

<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>
<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>
    <div class="modal-body">
        {% crispy feedback_form feedback_form.helper %}
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Submit</button>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

接下来,我有我的表格:

class Feedback(models.Model):
    creation_date = models.DateTimeField("Creation Date", default=datetime.now)
    topic = models.CharField("Topic", choices = TOPIC_CHOICES, max_length=50)
    subject = models.CharField("Subject", max_length=100)
    message = models.TextField("Message", blank=True)
    sender = models.CharField("Sender", max_length=50, blank=True, null=True)

    def __unicode__(self):
        return "%s - %s" % (self.subject, self.creation_date)

    class Meta:
            ordering = ["creation_date"]
        verbose_name = "Feedback"
        verbose_name_plural = "Feedback"

class Crispy_ContactForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                Field('topic', placeholder='Topic', css_class='input-medium'),
                Field('subject', placeholder='Subject', css_class='input-xlarge'),
                Field('message', placeholder='Message', rows='5', css_class='input-xlarge'),
                Field('sender', placeholder='Sender', css_class='input-xlarge'),
            ),
        )
        self.helper.form_id = 'id-Crispy_ContactForm'
        self.helper.form_method = 'post'

        super(Crispy_ContactForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Feedback
        exclude = ['creation_date']
Run Code Online (Sandbox Code Playgroud)

我试图以松脆的形式省略图例,因为如果包含它,则模态似乎有两个表单标题。但是,在松脆的表单布局中省略图例会导致字段出现乱序。

所以我有几个问题:

  1. 总的来说,我会以正确的方式来做吗?
  2. 如果我将模式的提交按钮连接到AJAX,该如何对表单进行错误检查?
  3. 有没有更好的方法可以在Bootstrap模式中显示脆皮形式?

Ed.*_*Ed. 5

在此页面上找到了部分解决方案。在基本模板中,我创建了按钮和表单:

<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 %}
Run Code Online (Sandbox Code Playgroud)

然后我创建了两个反馈表

<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>
Run Code Online (Sandbox Code Playgroud)

{%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>
Run Code Online (Sandbox Code Playgroud)

我将反馈表格分为两部分,因为我从以上链接中获取的bootstrap-ajax.js文件替换了一个模板中的html。如果我使用组合的反馈表,它将具有class =“ modal hide”。我需要它仅具有class =“ modal”,以便如果表单因错误而刷新,则模式不会消失。

我认为

@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")
Run Code Online (Sandbox Code Playgroud)

然后在bootstrap-ajax.js文件中(同样通过上面的链接),我做了一些更改。在processData函数中,我定义了:

var $el_parent = $el.parent();
Run Code Online (Sandbox Code Playgroud)

然后我加了

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);
}
Run Code Online (Sandbox Code Playgroud)

这还没有完全起作用,因为成功消息会立即随模式消失。我希望模式显示消息并在3秒钟后消失。还没有找到答案,但是目前效果很好。

我仍然在修补,但这可以解决我的大多数问题:

它使用AJAX提交数据,并在需要时返回并进行错误检查。该表格在模式中显示得相当好。

我还有一些问题。我需要找出一种方法来隐藏脆皮形式的图例,并且需要找到一种方法来显示模式脆皮形式,并且不干扰网站上其他地方出现的另一种脆皮形式。