开发者

Django ModelForm label captialisation

开发者 https://www.devze.com 2023-02-15 04:30 出处:网络
In my model I have; title = models.CharField(verbose_name=\"eBay Listing Title\",max_length=56) Using a ModelForm the label shows as \"EBay Listing Title\" (capital E). I\'m using

In my model I have;

title = models.CharField(verbose_name="eBay Listing Title",max_length=56)

Using a ModelForm the label shows as "EBay Listing Title" (capital E). I'm using

{{ field.label_tag }} 

on the form template (in a开发者_开发技巧 loop) to display the labels.

How can I get the label to show correctly with a lowercase first letter?


You can override the label in the form

for example:

class YourForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.fields['title'].label = "eBay Listing Title"

    class Meta:
        model = YourModel


Pass in the label argument http://docs.djangoproject.com/en/dev/ref/forms/fields/#label

The capitalization is just a default -- replacing underscores with spaces and capitalizing if you don't pass in anything.

Example from docs:

>>> class CommentForm(forms.Form):
...     name = forms.CharField(label='Your name')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号