【发布时间】: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类中使用!