Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django CreateView field labels

I'm working on a project that has a Chapter, with each Chapter having a title, content, and order. I'd like to keep the field 'order' named as is, but have the field displayed in a CreateView as something else, like 'Chapter number'. The best information I've found recommends updating the "labels" attribute in the Meta class, but this isn't working for me.

This is what I'm using now, which doesn't work:

class ChapterCreate(CreateView):
    model = models.Chapter
    fields = [
        'title',
        'content',
        'order',
    ]

    class Meta:
        labels = {
            'order': _('Chapter number'),
        }

I've also tried using the 'label's attribute outside of Meta, but that didn't work either. Should I be using a ModelForm instead, or is there a correct way to do this?

like image 255
brunosardinepi Avatar asked Nov 07 '16 22:11

brunosardinepi


People also ask

What does CreateView do in Django?

CreateView. A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object. This view inherits methods and attributes from the following views: django.

What is Django FormView?

FormView refers to a view (logic) to display and verify a Django Form. For example, a form to register users at Geeksforgeeks. Class-based views provide an alternative way to implement views as Python objects instead of functions.

What is generic editing?

Genome editing (also called gene editing) is a group of technologies that give scientists the ability to change an organism's DNA. These technologies allow genetic material to be added, removed, or altered at particular locations in the genome.


1 Answers

The simplest solution in this case would be to set the verbose_name for your model field

class Chapter(models.Model):
    order = models.IntegerField(verbose_name= _('Chapter number'))

Note I have use IntegerField in this example, please use whatever type is required.

like image 160
e4c5 Avatar answered Nov 15 '22 08:11

e4c5