将 CSS 应用于 Django 表单向导
Apply CSS to Django Form Wizard
我正在使用表单向导,它很好,但现在它不是很漂亮。
我的 Views.py 基于文档:
class ContactWizard(SessionWizardView):
form_list = [DurationForm, ContactForm2]
def done(self, form_list, **kwargs):
return render(self.request, 'done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
我的 done.html 包含:
{% extends "base.html" %}
{% block content %}
<h1> Done! </h1>
{% endblock %}
我的表单定义在 forms.py:
from django import forms
class DurationForm(forms.Form):
Duration_in_secounds = forms.IntegerField()
class FrequencyForm(forms.Form):
Frequency_in_time_interval = forms.IntegerField()
现在我想知道表单是如何呈现的,因为我从来没有按照我使用的方式加载它们(例如
{{form.as_p}}
因为我在 done.html 中所做的任何更改都不会影响使用向导创建的表单,所以我不知道如何向它们添加 css。
你们能帮我弄清楚这里发生了什么吗?
(很抱歉,如果这个问题很愚蠢/已经问过了 - 这里已经很晚了,过去两个小时我找不到任何东西)
我认为它是一个表单向导并不重要。您可以只创建一个基础 class,您可以从中扩展 DurationForm
和 ContactForm2
。例如:
def __init__(self, *args, **kwargs):
super(BaseFormClass, self).__init__(*args, **kwargs)
# specific field
self.fields['name'].widget.attrs['class'] = 'my_class'
# all fields on form
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'my_class'
未测试。
您应该将加载静态添加到您的模板 (html):
{% load staticfiles %}
<link href="{% static 'myapp/css/my.css' %}" rel="stylesheet">
这是加载 css、脚本等的地方。
我正在使用表单向导,它很好,但现在它不是很漂亮。
我的 Views.py 基于文档:
class ContactWizard(SessionWizardView):
form_list = [DurationForm, ContactForm2]
def done(self, form_list, **kwargs):
return render(self.request, 'done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
我的 done.html 包含:
{% extends "base.html" %}
{% block content %}
<h1> Done! </h1>
{% endblock %}
我的表单定义在 forms.py:
from django import forms
class DurationForm(forms.Form):
Duration_in_secounds = forms.IntegerField()
class FrequencyForm(forms.Form):
Frequency_in_time_interval = forms.IntegerField()
现在我想知道表单是如何呈现的,因为我从来没有按照我使用的方式加载它们(例如
{{form.as_p}}
因为我在 done.html 中所做的任何更改都不会影响使用向导创建的表单,所以我不知道如何向它们添加 css。
你们能帮我弄清楚这里发生了什么吗? (很抱歉,如果这个问题很愚蠢/已经问过了 - 这里已经很晚了,过去两个小时我找不到任何东西)
我认为它是一个表单向导并不重要。您可以只创建一个基础 class,您可以从中扩展 DurationForm
和 ContactForm2
。例如:
def __init__(self, *args, **kwargs):
super(BaseFormClass, self).__init__(*args, **kwargs)
# specific field
self.fields['name'].widget.attrs['class'] = 'my_class'
# all fields on form
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'my_class'
未测试。
您应该将加载静态添加到您的模板 (html):
{% load staticfiles %}
<link href="{% static 'myapp/css/my.css' %}" rel="stylesheet">
这是加载 css、脚本等的地方。