Default value in form

Hello

I would like to know if there is a way to show a default value in some form fields

For example, I have the user field that I would like to see already appear with the name of the user who is in session, also another date field and it will show the current date by default, as well as some others.

I understand that it should be put in the forms.py file but I can’t find much.

I show the form inside a modal.

Thank you.

1 Like

Checkout form initial values from the docs.

I don’t quite understand where to put it so that it comes out later in the form

I have the forms.py

class RecForm(forms.ModelForm):
    class Meta:
        model = Rec
        fields = ['fecha_reclamacion','num_reclamacion','motivo','aprueba', 'del_dep_code',]
        #fields = '__all__'
        num_reclamacion = forms.CharField(initial = "1234")
        labels = {
            'num_reclamacion': 'Nº Reclamación',
            'motivo': 'Motivo reclamación',
            'fecha_reclamacion': 'Fecha Reclamación',
            'aprueba': 'Aprueba',
        }

        widgets = {
            'num_reclamacion': forms.TextInput(
                attrs = {
                    'class': 'form-control',
                    'placeholder': 'Nº reclamacion',
                }
            )

I have added the line

num_reclamacion = forms.CharField(initial = "1234")

is it so?

That’s it.
But this may not be showing up, depending on how you are rendering the form on your template.

Do you mean modal?

<div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h2 class="modal-title">Creacion de Reclamación</h2>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <form id="form_creacion" action="{% url 'registrar_reclamacion' %}" method="POST">
            {% csrf_token %}
            <div class="modal-body">
                <div id="errores">

                </div>
                <div class="row">
                    <div class="col-8 col-sm-6">{{ form.fecha_reclamacion.label }} {{ form.fecha_reclamacion }}</div>
                    <div class="col-8 col-sm-6">{{ form.num_reclamacion.label }} {{ form.num_reclamacion }}</div>
                </div>
                <div class="row">
                    <div class="col-md-4"><br>{{ form.motivo.label }} {{ form.motivo }}</div>
                </div>
                <div class="row">
                    <div class="col-8 col-sm-6">{{ form.aprueba.label }} {{ form.aprueba }}</div>
                    <div class="col-8 col-sm-6">{{ form.del_dep_code.label }} {{ form.del_dep_code }}</div>
                </div>
                
            </div>
            <div class="modal-footer">
                <button class="btn btn-danger" type="button" onclick="cerrar_modal_creacion();">Cancelar</button>
                <button id="boton_creacion" class="btn btn-primary" type="button" onclick="registrar();">Confirmar</button>
            </div>
        </form>
    </div>
</div>

This should be declared outside the Meta class.

class RecForm(forms.ModelForm):
  num_reclamacion = forms.CharField(initial="1234")
  
  class Meta:
    # Continues

Correct, now yes.
And already linking to this, if in the name field I wanted to put the logged in username as the initial value, would that be possible?

This last one is not necessary, I already did it

def get_initial(self):
        return {"user": self.request.user}

Thank you