I want to create a custom name for on of the labels in my modelform this is my forms.py
class PostForm(forms.ModelForm): body = forms.CharField(widget=PagedownWidget) publish = forms.DateField( widget=forms.SelectDateWidget, initial=datetime.date.today, ) class Meta: model = Post fields = [ "title", "body", "author", "image", "image_url", "video_path", "video", "publish", "tags", "status" ]
I want to change the instead of video I want it to say embed. I checked the documentation but didn't find anything that would help me do that. is it possible without me having to rearrange my model? if so how? thanks
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.
label is used to change the display name of the field. label accepts as input a string which is new name of field. The default label for a Field is generated from the field name by converting all underscores to spaces and upper-casing the first letter.
From the documentation:
You can specify the labels, help_texts and error_messages attributes of the inner Meta class if you want to further customize a field.
There are examples just below that section of the docs. So, you can do:
class Meta: model = Post labels = { "video": "Embed" }
Yes, you can. Simply use the label
argument:
class PostForm(forms.ModelForm): ... video = forms.FileField(label='embed')
or define it inside your Meta
class:
class PostForm(forms.ModelForm): ... class Meta: ... labels = { "video": "embed" ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With