2015-11-08 51 views
0

這是forms.py:如何將整個CSS添加到django表單中?

from django.contrib.auth.models import User 
from django import forms 

class UserForm(forms.ModelForm): 
    class Meta: 
     model = User 
     fields = ('username', 'email', 'password') 
     widgets = { 
      'username': forms.TextInput(attrs={'class': ''}), 
      'email': forms.TextInput(attrs={'class': ''}), 
      'password': forms.TextInput(attrs={'class': ''}) 
    } 

我想的CSS添加到我的形式是這樣的:

<form method="post" action=""> 
{% csrf_token %} 
{{form.as_ul}} 
<div class="send"> 
    <input type="submit" name="register" value="Register" class="register" /> 
</div> 

我知道如何添加CSS類,但的最重要的部分CSS屬性被設置在這樣的整個文件:

.send input { 
    width: 27%; 
    height: 40px; 
    background: #0889c4; 
    border: 3px solid #0889c4; 
    color: #fff; 
    text-align: center; 
    cursor: pointer; 
} 

而且我不知道如何讓Django的仍作爲有效的CSS屬性。

回答

0

您可以使用attrs={'style':'width:150px;height:80px;','placeholder':'Some Text'}以及全部,爲任何單個字段填入任意數量的css屬性。如果你想將相同的css應用於表單中定義的字段組,而不是你所能做到的,那麼將該css類應用到你的html文件/模板中的<div>,並將表單的元素/字段放入該div中。它應該工作。

相關問題