skip to Main Content

after defining form field when I try to access in django template using {{form.Date_Joined}}

not working

class UserAdd(forms.ModelForm):
        
    class Meta:
        model=CompanyUsers
        fields= '__all__'
        exclude=['usrPsswd','usrId','usrCmpnyId','lstLogin','is_active','usrDOJ']
        widgets={

                
                'Date_Joined':forms.DateInput(attrs={'type':'date'}),
            }
        

The widget i’ve declare not showing

    <div class="order">
        <!-- <div class="head" > -->
             <h4> ADD COMPANY</h4>
        <!-- </div> -->
         <table id="table-datas">
            <form method="POST" action="">
                {% csrf_token %}
                <tbody>
           <tr><td>Username</td><br><td>{{ form.usrNme}}</td></tr>
           <tr><td>First Name </td><br><td>{{ form.usrFNme}}</td></tr>
           <tr><td>Last Name </td><br><td>{{ form.usrLNme}}</td></tr>
           <tr><td>Date of join</td><br><td>{{form.Date_Joined}}</td></tr>
           <tr><td>Department</td><br><td>{{ form.usrDpt}}</td></tr>
           <tr><td>Email</td><br><td>{{ form.usrMail}}</td></tr>
                    
           <tr><td><button type="submit">Register</button></td></tr>
           </form>```


so the Date Joined field shows nothing

2

Answers


  1. The type of the attrs is not taken into account, this originates from the input_type of the widget, you thus create a custom widget:

    class MyDateInput(forms.DateInput):
        input_type = 'date'

    then we plug this in as widget:

    class UserAdd(forms.ModelForm):
        class Meta:
            model = CompanyUsers
            fields = '__all__'
            exclude = [
                'usrPsswd',
                'usrId',
                'usrCmpnyId',
                'lstLogin',
                'is_active',
                'usrDOJ',
            ]
            widgets = {
                'Date_Joined': MyDateInput,
            }
    Login or Signup to reply.
  2. I think you make a mistake with the typo field of the form Date_Joined because python is case sensitive.

    Type

    widgets={
      'Date_Joined':forms.DateInput(attrs={'type':'date'}),
    }
    

    Instead of

    widgets={
      'date_Joined':forms.DateInput(attrs={'type':'date'}),
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search