Django model form widget not renderiing

Framework – Django. I am trying to create a contact form with input field attributes. I have a problem with the first field "name". The second to fourth fields come out perfectly in html. Here is my models.py:

from django.db import models
        class md_contactalpha(models.Model):
            name = models.CharField(max_length=100, verbose_name='Name',null=False,  blank=False  )
            email=models.EmailField(max_length=100,verbose_name='Email',
            unique=True,null=False,blank=False)
            phone = models.CharField( max_length=15, verbose_name='Phone',null=True,blank=True)
            message = models.CharField(max_length=512,verbose_name='Message',   null=True,
            blank=True, )               
            def __str__(self):
                return self.name

Here is the forms.py file:

from django import forms
from .models import md_contactalpha
class fm_contactalpha(forms.ModelForm):
    class Meta:
        model = md_contactalpha
        fields = ('name', 'email', 'phone', 'message') 
        widgets = {
            'name':forms.TextInput(attrs={'class':'input_text', 'placeholder': 'Full Name'}),
            'email':forms.EmailInput(attrs={'class':'input_text'}),
            'phone':forms.TextInput(attrs={'class':'input_text'}), 
            'message':forms.Textarea(attrs={'class':'message_box','rows':5}),   
            }

Here is the html file:

<form method="post">
        {% csrf_token %}      
            <form method="POST">
                      {{ form.as_p }}            
               </form>
<button type="submit">Send Message</button>

As mentioned the last 3 fields work perfectly. The “name” field does not fill any from the widget and produces this -

<input type="" class=  placeholder="Name" name="Name">
                        &lt;django.forms.fields.CharField object at 0x7fb2ea920ce0&gt;

I have checked other posts on the subject of widgets but there seems to be no answer for why one field called name should have a problem. I would greatly appreciate any assistance. Thank-you.

Looks like inner/outer <form> tag is not closed which cause such an unexpected behaviour. Try to remove line contains second <form method="POST"> tag.

<form method="post">
    {% csrf_token %}      
    {{ form.as_p }}            
</form>

Please, note :

  1. The submit button must be withing <form> tag to perform form submitting. Or, as alternative, must has an attribute form="yourFormId" to be able to submit particular form (something like : <button type="submit" form="yourFormID">Send Message</button>
  2. <form> tag without action= attribute will make a request to the same view form was rendered. If that is not what you expect - then action= attribute has to be set with url of view processes your form submit.
Back to Top