2015-05-18 77 views
0

當前試圖在django-crispy-forms中添加佔位符文本(灰色),但它無法顯示。有任何想法嗎?我已經嘗試了幾件事,但每次都未能在文本字段中生成任何輸出。Django香脆形式的佔位符文本沒有出現

from django import forms 
from .models import TaskItem 
from crispy_forms.bootstrap import AppendedText, PrependedText 
from crispy_forms.helper import FormHelper 
from crispy_forms.layout import Layout, ButtonHolder, Submit 
from crispy_forms.layout import Field 

class TaskItemForm(forms.ModelForm): 
    # task is changed to taskn 
    taskn = forms.CharField(max_length = 300) 
    def __init__(self, *args, **kwargs): 
     super(TaskItemForm, self).__init__(*args, **kwargs) 
     self.fields['taskn'].label = False 
     PrependedText('taskn', '@', placeholder="username") 
     self.helper = FormHelper() 
     self.helper.layout = Layout(
      'taskn', 
      ButtonHolder(
       Submit('Submit', 'Submit', css_class='btn-primary' , css_id = "floater") 
      ), 
      Field(placeholder = 'test' 
      ) 
     ) 

    # An inline class to provide additional information on the form. 
    class Meta: 
     fields = ('taskn',) 
     #This is the association between the model and the model form 
     model = TaskItem 
+0

你是如何呈現形式的模板? –

回答

0

你沒有足夠的Layout對象中的PrependedText

from django import forms 
from .models import TaskItem 
from crispy_forms.bootstrap import AppendedText, PrependedText 
from crispy_forms.helper import FormHelper 
from crispy_forms.layout import Layout, ButtonHolder, Submit 
from crispy_forms.layout import Field 

class TaskItemForm(forms.ModelForm): 
    class Meta: 
     model = TaskItem 
     fields = ('taskn',) 

    def __init__(self, *args, **kwargs): 
     super(TaskItemForm, self).__init__(*args, **kwargs) 

     self.helper = FormHelper() 
     self.helper.layout = Layout(
      PrependedText('taskn', '@', placeholder="username"), 
      ButtonHolder(
       Submit(
        'Submit', 
        'Submit', 
        css_class='btn-primary', 
        css_id='floater', 
       ) 
      ), 
     )