如何向 django 表单字段添加自定义标题?

问题描述 投票:0回答:2

我想将我自己的标题添加到 django 表单字段,如下图所示。

我的 models.py 文件

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')

还有我的 forms.py 文件

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

django
2个回答
4
投票

您可以在 model 级别上执行此操作,以便使用

ModelForm
 [Django-doc]
:
为该模型的 all
verbose_name=…

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

您还可以指定

labels
Meta
ModelForm
属性,在这种情况下,该标签当然仅适用于 that 特定形式,而对于同一型号的其他
ModelForm
,您可以因此必须重复标记:

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

我建议首先使用

verbose_name
,并且仅当表格需要偏离时,才为该特定
labels
指定
ModelForm


1
投票

终于找到答案了 将这些代码添加到 forms.py

 labels = {
        'ad_soyad': ('Ad Soyad'),
        'dogum_tarihi': ('Doğum Tarihi'),
    }
© www.soinside.com 2019 - 2024. All rights reserved.