Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field labels crispy forms Django

I want to use the same model for two forms and change the labels of field how can i change the label?

this is my one form:

class jobpostForm(forms.ModelForm):

    class Meta:

        model = jobpost
        fields = ('job_type','title','company_name','location','country','description','start_date','end_date','how_to_apply')

    widgets = {

        'job_type':RadioSelect(),    
        'location':TextInput(attrs={'size':'70','cols': 10, 'rows': 20}),   
        'description': TinyMCE(attrs={'cols':'100', 'row': '80'}),
            'start_date':AdminDateWidget(attrs={'readonly':'readonly'}),
            'end_date':AdminDateWidget(attrs={'readonly':'readonly'}),
            'how_to_apply':RadioSelect(),

    }

    def __init__(self, *args, **kwargs):
        super(jobpostForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'horizontal-form'
        self.helper.form_id = 'id-jobpostform'
        self.helper.form_class = 'blueForms'
        self.helper.form_method = 'post'

        self.helper.form_action = '/portal/next/post/'

        self.helper.add_input(Submit(_('submit_addcontent'), 'Preview'))


        super(jobpostForm, self).__init__(*args, **kwargs)

like i want to change 'location' to 'Job Location'..how can i do this?

like image 874
madeeha ameer Avatar asked Apr 29 '13 11:04

madeeha ameer


People also ask

What is crispy form tags in Django?

Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.


2 Answers

This problem is not really specific for Django Crispy Forms.

One option is to set the label in the init() method of your JobPostForm.

def __init__(self, *args, **kwargs):
    super(JobPostForm, self).__init__(*args, **kwargs)
    self.fields['location'].label = "Job Location"

A good read when dealing with these kind of questions is Overloading Django Form Fields.

like image 108
arie Avatar answered Oct 02 '22 06:10

arie


There is an easier way to do this. See my example below:

    class CreateAPIKey(forms.ModelForm):

    class Meta:
        model = APIKey
        fields = ["client_id"]
        labels = {
            "client_id": "Nome da Key",
        }
        help_texts = {
            "client_id": _(
                "Um identificador exclusivo de formato livre da chave. 50 caracteres no máximo"
            ),
        }
        widgets = {
            "client_id": forms.TextInput(
                attrs={
                    "id": "key_id",
                    "required": True,
                    "placeholder": "Entre um nome único para a chave",
                    "label": "dasdasdsdasd",
                }
            ),
        }

Then, then rendering the form on the template:

<form class="" method="POST" action="#">
{% csrf_token %}
  <div class="col-md-4 col-sm-12 form-group" data-children-count="1">

    {% for field in form %}
       {{ field|as_crispy_field }}
    {% endfor %}
                                                        
  </div>
</form>

RESULT:

Result of the above code

like image 30
Leonardo Guerreiro Avatar answered Oct 02 '22 05:10

Leonardo Guerreiro