我想将我自己的标题添加到像这张图片这样的 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'}),
}
uj5u.com热心网友回复:
您可以在模型级别执行此操作,以便使用[Django-doc]为该模型的所有 s 完成此操作:ModelForm
verbose_name=…
class Post(models.Model):
# …,
dogum_yeri = models.CharField(verbose_name='Do?um Tarihi', max_length=255, default="")
# …
您还可以指定 的labels
属性Meta
,ModelForm
在这种情况下,此标签当然仅适用于该特定形式,而对于ModelForm
同一模型的其他 s,您将不得不重复标记:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__'
widgets = {
# …
}
labels = {
'dogum_yeri': 'Do?um Tarihi'
}
我建议先使用verbose_name
,并且仅当表单需要偏离时,才labels
为特定的ModelForm
.
uj5u.com热心网友回复:
我终于找到了答案 将这些代码添加到 forms.py
labels = {
'ad_soyad': ('Ad Soyad'),
'dogum_tarihi': ('Do?um Tarihi'),
}
0 评论