Django widgets

Use of internal widgets

  ” inherit “forms.Form

  1. Basic Edition

Specifies the type of control only.

label=’Label ‘,

widget=forms.)

Eg:()

Upwd=forms.Charfield(

label=’User password:

widget=forms.PasswordInput

)

    2. > attribute

label=’Label ‘,

widget=forms.attrs={

 ‘html,

 ‘html  }

)

The following example is just a simple use of widgets, and no real validation is done.

widget.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Demonstration of widgets</title>
 6 </head>
 7 <body>
 8 <form action="/13_widget/" method="post">
 9     {% csrf_token %}
10     {{ form.as_p }}
11     <p>
12         <input type="submit" value="Submission " >
13 
14     </p>
15 
16 </form>
17 </body>
18 </html>

forms.py

class WidgetForm(forms.Form):
    uname=forms.CharField(
        label='User name',
        widget=forms.TextInput(
            attrs={
                'name':'user name',
                'placeholder':'Please enter user name:',
                'class':'form-control',
            }
        )
    )

    upwd = forms.CharField(
        label='User password',
        widget=forms.PasswordInput(
            attrs={
                'name': 'user_password',
                'placeholder': 'Please enter user password:',
                'class': 'form-control',
            }
        )

    )

views.py

def widget_view(request):
    form=WidgetForm()
    return render(request,'widget.html',locals())

urls.py

#Widget
    path('13_widget/', widget_view)

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *