Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove Label text in Django generated form?

I have a form that is displaying well only for the label text that I don't want and I have tried all I could to let it off my form but it won't just go...

forms.py:

class sign_up_form(forms.ModelForm):
    class Meta:
        model = Users
        fields =['email']
        widgets = {
            'email': forms.EmailInput(attrs={
                'id': 'email',
                'class': 'form-control input-lg emailAddress',
                'name': 'email',
                'placeholder': 'Enter a valid email'})}

I have tried: views.py:

from django.shortcuts import render
from mysite.forms import sign_up_form

def register(request):
    sign_up = sign_up_form(auto_id=False)
    context = {'sign_up_form': sign_up}
    return render(request, 'mysite/register.html', context)

I need my widgets as defined above.

like image 662
Yax Avatar asked Sep 14 '14 23:09

Yax


People also ask

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is form Cleaned_data in Django?

form. cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

How do I stop multiple form submissions in Django?

Multiple form submission happens because when page refreshes that same url hits, which call that same view again and again and hence multiple entries saved in database. To prevent this, we are required to redirect the response to the new url/view, so that next time page refreshes it will hit that new url/view.

How do I delete a form in Django after submitting?

It's standard to redirect after form submission to prevent duplicates. Just return a redirect to your form on success. after save() you can return 'form' key with MessagesForm(request.

What is a form in Django?

In the context of a web application, ‘form’ might refer to that HTML <form>, or to the Django Form that produces it, or to the structured data returned when it is submitted, or to the end-to-end working collection of these parts. At the heart of this system of components is Django’s Form class.

How to validate every field in Django?

Every field comes in with some built-in validations from Django validators. Each Field class constructor takes some fixed arguments. label is used to change the display name of the field. label accepts as input a string which is new name of field.

How do you handle errors for hidden fields in Django?

For example, because hidden fields don’t display anything, putting error messages “next to” the field could cause confusion for your users – so errors for those fields should be handled differently. Django provides two methods on a form that allow you to loop over the hidden and visible fields independently: hidden_fields () and visible_fields ().

How to initiate a Django form from a URL?

Now, to initiate a Django form you need to create home.html where one would be designing the stuff as they like. Let’s create a form in home.html. Finally, a URL to map to this view in urls.py


1 Answers

In ModelForms there will be default labels so you have to over-ride where you don't need labels

you can define it like this

class sign_up_form(forms.ModelForm):
    email = forms.CharField(widget=forms.Textarea, label='')
    class Meta:
        model = Users
        fields =['email']

This method will not include labels for your form, other method depends on rendering in template. You can always avoid labels from form <label>MY LABEL</label> instead of {{ form.field.label }}

like image 142
Nitish Kansal Avatar answered Sep 23 '22 11:09

Nitish Kansal