[英]How do I label fields on a Django form?
我試圖弄清楚如何 label 我的 Django 表單字段 - 目前我無法更改它們。 我曾嘗試在models.py 中修改字段名稱並添加標簽,但它會引發錯誤,我不確定在哪里添加它們。
模型.py:
from django.db import models
from django.contrib.auth.models import User
class Stats(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now=True)
weight = models.DecimalField(max_digits=5, decimal_places=2)
run_distance = models.IntegerField(default=5)
run_time = models.TimeField()
class Meta:
db_table = 'health_stats'
ordering = ['-date']
def __str__(self):
return f"You currently weigh {self.weight}, {self.user}"
視圖.py:
class UpdateHealth(View):
def get(self, request, *args, **kwargs):
stats = Stats
update_form = StatUpdateForm
context = {
'stats': stats,
"update_form": update_form,
'user': stats.user,
'weight': stats.weight,
'date': stats.date,
}
return render(request, 'health_hub_update.html', context)
def post(self, request, *args, **kwargs):
stats = Stats
update_form = StatUpdateForm(data=request.POST)
context = {
'stats': stats,
"update_form": update_form,
'user': stats.user,
'weight': stats.weight,
'date': stats.date,
'run time': stats.run_time,
'run distance': stats.run_distance
}
if update_form.is_valid():
update_form.save()
return render(request, 'health_hub_update.html', context)
forms.py:
class StatUpdateForm(forms.ModelForm):
class Meta:
model = Stats
fields = ('user', 'weight', 'run_distance', 'run_time')
任何幫助,將不勝感激!
嘗試這個:
#forms.py
class StatUpdateForm(forms.ModelForm):
class Meta:
model = Stats
fields = ('user', 'weight', 'run_distance', 'run_time')
def __init__(self, *args, **kwargs):
super(StatUpdateForm, self).__init__(*args, **kwargs)
self.fields['user'].label = "New user Label"
self.fields['weight'].label = "New weightLabel"
self.fields['run_distance'].label = "New run_time Label
或者
解決方案二:
將 label 屬性添加到字段。
#models.py
class Stats(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, label="User Label"))
date = models.DateField(auto_now=True, label="Date Label"))
weight = models.DecimalField(max_digits=5, decimal_places=2)
run_distance = models.IntegerField(default=5)
run_time = models.TimeField()
試試這個,它適用於 django 4.1
class ProductForm(forms.ModelForm):
class Meta:
model = ProductModel
fields = ('name', 'og_price', 'discount', 'sell_price', 'dis_pice', 'info', 'status', )
labels = {
'name':'Product Name',
'og_price':'Original Price',
'discount':'Product Discount',
'sell_price':'Product Selling Price',
'dis_pice':'Product Discounted Price',
'info':'Product Information',
'status':'Product Availability Status',
}
By default in case of model forms The form field's label is set to the verbose_name of the model field, with the first character capitalized.
如果您想要對表單標簽進行任何自定義,您可以以字典形式傳遞標簽,其中鍵作為字段名稱,值作為 model 表單的 Meta class 中的自定義 label 值。
例如:
labels = {‘name’: ‘Enter Name’, ‘password’: ‘Enter Password’, ‘email’: ‘Enter Email’ }
class StatUpdateForm(forms.ModelForm):
class Meta:
model = Stats
fields = [‘name’, ‘password’, ‘email’]
labels = {‘name’: ‘Enter Name’, ‘password’: ‘Enter Password’, ‘email’: ‘Enter Email’ }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.