Issue while using POST method in form

Hi everyone !

I’m trying to use a “create customer” form.

models.py

    class CustomerModel(models.Model):
    customer_first_name = models.CharField(max_length=40, default="")
    customer_surname = models.CharField(max_length=40, default="")
    customer_orders = models.ForeignKey(OrderModel, related_name="customer_orders",on_delete=models.CASCADE)

    def __str__(self):
        return self.customer_first_name + self.customer_surname

forms.py

    class CustomerForm(ModelForm):
    class Meta:
        model = CustomerModel
        fields = '__all__'

create_customer.html

    <div id="create_customer_box">
    <p id="create_customer_box_title">Create a Customer</p>
    <form id="create_customer_form" action="" method="POST">
        {% csrf_token %}
        <label type="text" for="customer_name">Name : </label>
        <br>
        {{ form.customer_first_name }}
        <br>
        <label type="text" for="customer_surname">Surname : </label>
        <br>
        {{ form.customer_surname }}
        <br>
        <input type="submit" class="button_style_as_input" value="Create">
    </form>
</div>

When I hit the “Create” button in create_customer.html I get :

Environment:


Request Method: POST
Request URL: http://localhost:8000/customers/create_customer

Django Version: 3.0.5
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'dashboard.apps.DashboardConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Théo\Documents\Web\myDashboard\dashboard\views.py", line 40, in create_customer
if(form.is_valid()):
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 180, in is_valid
return self.is_bound and not self.errors
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 175, in errors
self.full_clean()
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 376, in full_clean
self._clean_fields()
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\forms.py", line 388, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
  File "C:\Users\Théo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\forms\widgets.py", line 258, in value_from_datadict
return data.get(name)

Exception Type: AttributeError at /customers/create_customer
Exception Value: 'str' object has no attribute 'get'

I really don’t know how to consider this error message…

Thanks in advance for your help

EDIT :

After having debugged my code, I noticed that the error was in my views.py file :

def create_customer(request):
if request.method == "POST":
    form = CustomerForm(request.method) => Should be request.POST instead

    if form.is_valid():
        form.save()
        return redirect("/")
        
else:
    form = CustomerForm()

template_name = "dashboard/create_customer.html"
context = {
    "form":form,
}

return render(request,template_name,context)