[Answered ]-Django form is not creating new entries

1👍

I tried replicating your project twice, both times I had no issue copying your code directly. It works fine for me. I set up a couple of print statements in the view:

from django.shortcuts import render
from .forms import NewClientForm
from .models import Client

# Create your views here.
def new_client_view(request):
    new_client_form = NewClientForm()
    print('request method: ', request.method)
    if request.method == "POST":
        new_client_form = NewClientForm(request.POST)
        if new_client_form.is_valid():
            Client.objects.create(**new_client_form.cleaned_data)
            print('new client created')
        else:
            print(new_client_form.errors)
    context = {
    "form": new_client_form
    }
    return render (request, 'clients/new-client.html', context)

This is what I see on the new client form page:

new client form page

This is what I see on the command line as I visit the page and submit the form:

cmd line

As you can see, once I visit the page, the view receives a GET request, which makes sense, and which I see from your comment that you’re seeing too. Once I hit "Submit" the view receives a POST request, which then creates a new Client object.

I used the Django shell to confirm that the Client object was indeed created:

django shell

So your code is fine, it may have something to do with the way you’re filling up your form, or maybe a browser issue, I can’t really tell.

Leave a comment