2016-06-04 2 views
1

양식에 자리 표시 자 특성을 추가하는 데 문제가 있습니다. 내 HTML 모양을 표시하는 자리 표시자를 추가하고 싶습니다.Django에서 자리 표시 자 특성을 추가하는 방법

<input type="text" name="sample" placeholder="sample"> 

지금 이것은 내 코드 모양입니다.

내 모델입니다 :

from django.db import models 

class Sample(models.Model): 

    name = models.CharField("Name", max_length=120) 

이 내 양식이다 : 그것은 작동하지 않는 속성 내가 자리 표시자를 포함 할 때

from django.forms import ModelForm, TextInput 
from .models import Sample 

class SampleForm(ModelForm): 

    class Meta: 
     INPUT_CLASS = 'form-control' 

     model = Sample 
     widgets = { 
      'name': TextInput(attrs={'class':INPUT_CLASS, 
            'placeholder':'Enter title here'}), 
     } 

보기 내 형태로

def update(request): 

    sampleForm = SampleForm() 

    return render(request, 'sample/profile.html', 'form':sampleForm) 

. 이 방법을 사용하는 최선의 방법은 무엇입니까?

형태 자체
+0

어떻게 양식을 렌더링하는 ... 당신의 코드가 작동하지 않는 이유의 질문에 대답하지 않는 이유는 무엇입니까? 관련 템플릿 코드를 게시하십시오. – solarissmoke

답변

0

이 그것은 조금 늦게입니다 항상

class SampleForm(ModelForm): 
    class Meta: 
     model = Sample 

    name = forms.CharField(
     label='Name', 
     widget=forms.TextInput(
      attrs={'class': 'form-control', 'placeholder': 'Enter title here'} 
     ) 
    ) 
+0

OP의 원래 코드가 작동하지 않는 이유는 무엇입니까? – solarissmoke

+0

@solarissmoke 나는 아직 모릅니다. – doniyor

1

나를 위해 작동하지만, 어쨌든에서 칩 것이다. 양식에 사용 된 위젯을 다시 정의하지 않고도 동일한 작업을 수행하려고했습니다. 발견 된 그 다음 작품 :

class ExampleForm(forms.ModelForm): 

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

     # sets the placeholder key/value in the attrs for a widget 
     # when the form is instantiated (so the widget already exists) 
     self.fields['name_of_field'].widget.attrs['placeholder'] = 'random placeholder' 

편집 : 나는 실현 있지만이