Form return None Value

Hi Guys,

When a user fill out a form, sometime, he do not put an information.
So, the field is empty.

The result in Django (Reuest.POST Form) is a None value.
Is there a way to change this None value to an empty value : '"" ?

In models ? in forms ?in views ??

Thanks for your help

Hi thais781,

If you’re using a form class this should be done automatically for you if it’s a CharField as per these docs.

Can you share the form and view for this? And can you determine if the field is being included in the POST? You can find that in the browser’s development tools’ network panel.

Hi Tim

Thanks for your help
you mean I just have to add ‘required=False’ to my models ?
but i hav this error : TypeError: init() got an unexpected keyword argument ‘required’

I give you a simplify version(one field for easy reading) of my structure :
class Peoples_Main(models.Model):
   name_last = models.CharField(max_length = 100, blank=True, null=True)
class Peoples_Etat_Civil_Form(ModelForm):
	class Meta:
		model = Peoples_Main
		fields = ['name_last']
my_peoples_etat_civil_form = Peoples_Etat_Civil_Form(instance=my_people_main)

if request.method == 'POST':
	my_temp = Peoples_Etat_Civil_Form(request.POST,instance=my_people_main)
	if my_temp.is_valid():

<div class="input-group py-1">
{% render_field my_peoples_etat_civil_form.name_last class="form-control" placeholder="[-- Nom --]" %}

Sorry, I am a french girl so somme word in my code are in french

No worries! I shouldn’t have to understand those variables/members to help you.

So what I think is happening is that passing the instance is what is causing the value to be retained as None rather than be converted to "". If you’re trying to default to an empty string, is there a reason why you want that field to support NULLs in the database?

1 Like

Also, in addition to @CodenameTim 's question about the NULLs, can we see the complete view? (My question involves how “my_people_main” is being created or accessed?)