How to add custom title to django form fields?

I wanna add my own title to django form fields like this image.

My models.py file

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

class Post(models.Model):
    ad_soyad = models.CharField(max_length=255, default="")
    tarih = models.CharField(max_length=255, default="")
    dogum_tarihi = models.CharField(max_length=255, default="")
    dogum_yeri = models.CharField(max_length=255, default="")
    medeni_hali = models.CharField(max_length=255, default="")
    birinci_evlilik = models.CharField(max_length=255, default="")
    ikinci_evlilik = models.CharField(max_length=255, default="")
    bosanma_tarihi = models.CharField(max_length=255,  default="")
    ogrenim_durumu = models.CharField(max_length=255, default="")
    meslek = models.CharField(max_length=255, default="")
    kan_grubu = models.CharField(max_length=255, default="")
    boy = models.CharField(max_length=255, default="")
    kilo = models.CharField(max_length=255, default="")
    max_kilo = models.CharField(max_length=255, default="")
    max_kilo_tarih = models.CharField(max_length=255, default="")
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()

    def __str__(self):
        return self.title + '|' + str(self.author)


    def get_absolute_url(self):
    #    return reverse('article-detail', args=(str(self.id)))   bu kod olursa yazı ekledikten sonra o eklenen yazıya gidiyor
         return reverse('home')

And my forms.py file

from django import forms
from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'
        #field = ('title', 'author')

        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'author': forms.Select(attrs={'class': 'form-control'}),
            'body': forms.Textarea(attrs={'class': 'form-control'}),
            'ad_soyad': forms.TextInput(attrs={'class': 'form-control'}),
            'tarih': forms.TextInput(attrs={'class': 'form-control'}),
            'dogum_tarihi': forms.TextInput(attrs={'class': 'form-control'}),
            'dogum_yeri': forms.TextInput(attrs={'class': 'form-control'}),
            'medeni_hali': forms.TextInput(attrs={'class': 'form-control'}),
            'birinci_evlilik': forms.TextInput(attrs={'class': 'form-control'}),
            'ikinci_evlilik': forms.TextInput(attrs={'class': 'form-control'}),
            'bosanma_tarihi': forms.TextInput(attrs={'class': 'form-control'}),
            'ogrenim_durumu': forms.TextInput(attrs={'class': 'form-control'}),
            'meslek': forms.TextInput(attrs={'class': 'form-control'}),
            'kan_grubu': forms.TextInput(attrs={'class': 'form-control'}),
            'boy': forms.TextInput(attrs={'class': 'form-control'}),
            'kilo': forms.TextInput(attrs={'class': 'form-control'}),
            'max_kilo': forms.TextInput(attrs={'class': 'form-control'}),
            'max_kilo_tarih': forms.TextInput(attrs={'class': 'form-control'}),
        }

enter image description here


Solution 1:

You can do this on the model level, such that this is done for all ModelForms for that model with the verbose_name=… [Django-doc]:

class Post(models.Model):
    # …,
    dogum_yeri = models.CharField(verbose_name='Doğum Tarihi', max_length=255, default="")
    # …

You can also specify the labels attribute of the Meta of the ModelForm, in that case this label of course only applies to that specific form, and for other ModelForms for the same model, you thus will have to repeat labeling:

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'
        widgets = {
            # …
        }
        labels = {
            'dogum_yeri': 'Doğum Tarihi'
        }

I would advise to use verbose_name first, and only if a form needs to deviate, specify the labels for that specific ModelForm.

Solution 2:

I found the answer finally Adding these code to forms.py

 labels = {
        'ad_soyad': ('Ad Soyad'),
        'dogum_tarihi': ('Doğum Tarihi'),
    }