Django Form, form is not being submitted

Viewed 30

I am pretty new to django and I am having some issues with my form. It is not submitting anything. I don´t have any idea why, no issue appears in the terminal. It displays the form correctly, but when filling it out and submitting, it just redirects me to the same form but blank. I check the database and nothing´s been added. My code below:

#views.py
def ContractView(request):
   form=contractsform(request.POST)
   if form.is_valid():
        con =form.save()
        return redirect("{% url 'contracts' %}", con.id)
   else:
        form = contractsform()
   return render(request, 'contform.html', {'form': form})

#contform.html
                  <div class="card-body">
                    <form action="" method="POST" class="row g-3">
                        {% csrf_token %}
                        <label for="{{ form.subject.id_for_label }}">Name:</label>
                        {{form.name}}
                        <div class="col-md-6">
                            <label for="{{ form.subject.id_for_label }}">Contractor:</label>
                            <div class="input-group input-group-sm mb-3">
                                {{form.contractor}}
                                <button id="new-vendor" class="btn btn-outline-secondary" type="button">+</button>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label for="{{ form.subject.id_for_label }}">Contractee:</label>
                            {{form.contractee}}
                        </div>
...
                        <div class="col-md-6">
                            <button type="button" onclick="javascript:history.back()">Cancel</button>
                        </div>
                        <div class="col-md-6">
                            <input type="submit" value="Submit" class="btn btn-primary" style="float: right;">
                        </div>

                    </form>

#forms.py
class contractsform(forms.ModelForm):
    class Meta:
        model = Contratos
        fields = '__all__'

        widgets = {
            
            'name': forms.TextInput(attrs ={'class': 'form-control'}),
            'contractee': forms.TextInput(attrs={'class': 'form-control'}),
            'contractor': forms.Select(attrs={'class': 'form-control', 'id': 'contractor_view' }),}

#urls.py
urlpatterns = [
    path('contracts/', views.contratostabla, name='contracts'),
    path('contracts/add/', ContractView, name='new-contract'),
]
2 Answers

You are currently redefining the form as an empty form when it is not valid, you need to change you logic to use a form filled with POST data when the request method is POST and an empty form when the method is GET. You still need to use the filled form for POST when it's invalid so that you get the errors and the previous data

def ContractView(request):
   if request.method == 'POST':
       form = contractsform(request.POST)
       if form.is_valid():
           con = form.save()
           return redirect("{% url 'contracts' %}", con.id)
   else:
        form = contractsform()
   return render(request, 'contform.html', {'form': form})

FYI on readability, the convention is to use CamelCase for classes so your form would be named ContractsForm and lowercase-with-underscores for functions so your view would be contract_view

You can update your view like this

def ContractView(request):
   form = contractsform(request.POST or None)
   if form.is_valid():
        con = form.save()
        con.save()
        return redirect("{% url 'contracts' %}", con.id)
   else:
        form = contractsform()
   return render(request, 'contform.html', {'form': form})

I think this will solve your problem

Related