我正在尝试用crispy表单为我的网站创建一个响应表单,我没有使用bootstrap,我想在crispy表单中添加自定义CSS来匹配我的整个网站。
HTML格式:

<form method='POST' action=''>{% csrf_token %}

            {{ form|crispy }}

                    <input type='submit' value='Sign Up' />



            </form>

使用inspect元素可以显示:
            <form method='POST' action=''><input type='hidden' name='csrfmiddlewaretoken' value='GLJMxnnDz1MGNFC46pjoofSlo6JMCD1IXu7X3n7LsRbQfdS38SYHJMs9IAXddcck' />



<div id="div_id_full_name" class="form-group"> <label for="id_full_name" class="control-label ">
                Full name
            </label> <div class="controls "> <input class="textinput textInput form-control" id="id_full_name" maxlength="120" name="full_name" type="text" /> </div> </div> <div id="div_id_email" class="form-group"> <label for="id_email" class="control-label  requiredField">
                Email<span class="asteriskField">*</span> </label> <div class="controls "> <input class="emailinput form-control" id="id_email" maxlength="254" name="email" type="email" required /> </div> </div>


        <!--    <input class='btn btn-primary' type='submit' value='Sign Up' />-->
                        <input type='submit' value='Sign Up' />



            </form>

forms.py格式:
from django import forms

from .models import SignUp

class ContactForm(forms.Form):
    full_name = forms.CharField(required=False)
    email = forms.EmailField()
    message = forms.CharField()


class SignUpForm(forms.ModelForm):
    class Meta:
        model = SignUp
        fields = ['full_name', 'email']

型号.py:
from __future__ import unicode_literals

# Create your models here.
from django.db import models

# Create your models here.
class SignUp(models.Model):
    email = models.EmailField()
    full_name = models.CharField(max_length=120, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self): #Python 3.3 is __str__
        return self.email

最佳答案

作为docs says,默认情况下使用bootstrap的crispy表单,还为bootstrapbootstrap3bootstrap4uni-form提供了一些模板包。另请参见关于Overriding project templates
如果您需要定制这个灵活的表单,您需要为您的项目创建一个新的自定义模板,例如crispy_forms/templates/<foobar>/。您可以在以下存储库路径签出:https://github.com/django-crispy-forms/django-crispy-forms/tree/dev/crispy_forms/templates
但是,以前的crispy表单有模板标记来处理特定字段。其中之一是{{ form.field_name|as_crispy_field }},下面的示例是它的输出。

<div id="div_id_email" class="control-group">
  <label for="id_email" class="control-label">Email address</label>
  <div class="controls">
    <input class="form-control" id="id_email" maxlength="254" name="email" required="required" type="email" />
  </div>
</div>

其他选项,您可以使用表单小部件中的特定html选择器/属性来处理它,例如htmlclassidstyle或其他。
比如你的情况;
class SignUpForm(forms.ModelForm):

    class Meta:
        model = SignUp
        fields = ['full_name', 'email']
        widgets = {
          'email': forms.EmailInput(attrs={'class': 'form-control custom-class'}),
        }

然后,如果呈现为{{ form.email|as_crispy_field }},则应呈现html;
<div id="div_id_email" class="control-group">
  <label for="id_email" class="control-label">Email address</label>
  <div class="controls">
    <input class="form-control custom-class" id="id_email" maxlength="254" name="email" required="required" type="email" />
  </div>
</div>

Crispy forms还提供简单的settings.py内部配置,Django字段生成默认类,Crispy forms处理这些类并添加其他类以与CSS框架兼容。例如aCharField生成一个<input class="textinput"for more...
CRISPY_CLASS_CONVERTERS = {
    'textinput': "form-control cst__radius",
    'urlinput': "form-control cst__radius",
    'numberinput': "form-control cst__radius",
    'emailinput': "form-control cst__radius",
    'dateinput': "form-control cst__radius",
    'textarea': "form-control cst__radius",
    'passwordinput': "form-control cst__radius",
    'select': "form-control cst__radius",
}

07-24 14:57