【问题标题】:Django Crisy Form Layout Does Not WorkDjango Crisy 表单布局不起作用
【发布时间】:2017-08-18 13:11:14
【问题描述】:

我正在尝试使用 Django Crisy Form 在灯箱中构建联系表单。但是,我无法让布局或 css 工作,因为结果在原始 html 格式中是相同的。我附上下面的代码,如果有人能建议如何让它工作,我将不胜感激。非常感谢。

模型.py:

from django.db import models

# Create your models here.
class db_contact(models.Model):
    Date = models.DateTimeField ('Date')
    Name = models.CharField ('Name', max_length = 50)
    Subject = models.DateField ('Subject', max_length = 20)
    Email = models.CharField ('Email', max_length = 200)
    Message = models.CharField ('Message', max_length = 50000)

    def __str__(self):
        return self.Subject

    class Meta:
        ordering = ['id']

Forms.py

from django import forms
from django.forms import ModelForm
from siteapps.models import db_contact
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit

class frm_contact(ModelForm):       

    def __init__(self, *args, **kwargs):
        super(frm_contact, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)    
        self.helper.form_id = 'id-exampleForm'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'
        self.helper.form_action = 'submit_survey'

        self.helper.add_input(Submit('submit', 'Submit'))


    class Meta:
        model = db_contact   
        fields = ['Name', 'Subject', 'Email', 'Message']

views.py

def contact(request):
    form_contact = frm_contact()
    return render(request, 'contact.html', {'form_contact' : form_contact}) 

contact.html

{% extends request.is_ajax|yesno:"fancybox/base.html,main.html" %}
{% load crispy_forms_tags %}
{% load i18n %}

{% block content %}

    {% crispy form_contact form_contact.helper %}

    <script src='https://www.google.com/recaptcha/api.js'></script>
    <div class="g-recaptcha" data-sitekey="xxx"></div>
{% endblock %}

【问题讨论】:

  • 你已经导入了Layout,但是没有在Form类中使用!

标签: css django-crispy-forms


【解决方案1】:

我将在找到解决方案后回答我的问题。

原来我错过了放置 bootstrap js/css,现在它可以与 bootstrap3 一起使用。

可以在此处找到答案的更多详细信息: Django crispy forms not loading CSS

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2015-12-03
    • 2019-01-26
    • 2020-07-07
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多