2016-03-10 6 views
0

Я переформатировал форму для связи с нами, это стандарт django-form, но я изо всех сил пытаюсь понять, как написать описание внутри поля <inpout>.Форма ввода ввода Django

Чтобы быть точным, я хочу написать Name внутри поля <input>, так как я могу это сделать. Я определяю <input> поле как {{ form.name }}, так как я могу определить его больше как <input type="name" class="form-control" id="id_name" placeholder="Your Name">.

<div class="col-xs-12 col-sm-12 col-md-4 col-md-offset-2 col-lg-4"> 
     {% if form.errors %} 
      <p style="color: red"> 
       Please correct the error{{ form.errors|pluralize }} below. 
      </p> 
     {% endif %} 

      <form class="form-horizontal" action="." method="POST"> 
       {% csrf_token %} 
       <div class="field form-group"> 
        {{ form.name.errors }}       
        <label for="id_name" class="control-label"></label> 
        <div class="col-sm-10">  
         {{form.name}} 
        </div> 
       </div> 
       <div class="field form-group"> 
        {{ form.email.errors }}       
        <label for="id_email" class="control-label"></label> 
        <div class="col-sm-10"> 
         {{ form.email }} 
        </div> 
       </div> 
       <div class="field form-group"> 
        {{ form.phone_number.errors }}       
        <label for="id_phone_number" class="control-label"></label> 
        <div class="col-sm-10"> 
         {{ form.phone_number }} 
        </div> 
       </div> 
       <div class="field form-group"> 
        {{ form.message.errors }}       
        <label for="id_message" class="control-label"></label> 
        <div class="col-sm-10"> 
         {{ form.message }} 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-12"> 
         <input type="submit" class="btn btn-primary btn-block" value="Submit"></button> 
        </div> 
       </div> 
      </form> 
      </div><!--/end form colon --> 
+0

[Этот вопрос] (http://stackoverflow.com/questions/401025/define-css-class-in-django-forms) должно помочь. – Alasdair

+0

Вы также можете использовать стиль в скрипте: $ ('# id_form.message'). AddClass() – Aquiles

ответ

3

If you want to modify the placeholder text, or other values of the field Django creates, it can be done in the form code using the widget's attrs:

class MyForm(forms.Form): 
    name = forms.CharField(
     widget=forms.TextInput(
      attrs={ 
       'class': 'my-class', 
       'placeholder': 'Name goes here', 
      })) 

Если ваше намерение сохранить весь код в шаблоне вам придется вручную создать каждое поле ввода вместо того, чтобы использовать те, Django делает. You can still access the values of the generated field in the template to help you do that though.

<input name="{{ form.name.name }}" type="text" placeholder="Name field"> 
+0

Я надеялся избежать использования back-hand codding, есть ли возможность сделать это внутри шаблона django. – PetarP

+0

Это хороший пример, я пропустил это в документах, но я хотел бы, чтобы 'placeholder' жил внутри' django-templates' не 'django-forms'. – PetarP

+0

Выполнено, вручную. – PetarP

Смежные вопросы