D
D
dintear_koden2020-02-23 16:12:20
Django
dintear_koden, 2020-02-23 16:12:20

How to add a placeholder to a model form?

How to add placeholder to inputs?

forms.py file

from django.forms import ModelForm
from .models import Question


class QuestionForm(ModelForm):
    class Meta:
        model = Question
        fields = '__all__'


models.py file
from django.db import models

class Question(models.Model):
  name = models.CharField(max_length=100)
  phone = models.CharField(max_length=20)
  mail = models.CharField(max_length=200)
  question = models.TextField()

  def __str__(self):
    return self.name


index.html file
<form action="" method="POST">
            {% csrf_token %}

            <div class="row">
              <div class="col-lg-6">
                <p class="input-field">
                  {{ form.name }}
                </p>
                <p class="input-field">
                  {{ form.phone }}
                </p>
                <p class="input-field">
                  {{ form.mail }}
                </p>
              </div>
              <div class="col-lg-6">
                <p class="input-field">
                  {{ form.question }}
                </p>
                <p class="input-field">
                  <button type="submit" class="send-qtn">Отправить <i class="far fa-paper-plane"></i></button>
                </p>
              </div>
            </div>
          </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-23
@dintear_koden

class QuestionForm(ModelForm):
    class Meta:
        model = Question
        fields = '__all__'
        widgets = {
            'field_name': TextInput(attrs={'placeholder': 'some value'}),
        }

And don't use fields = '__all__', it's bad practice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question