Search code examples
python-3.xdjangodjango-modelsdjango-viewsdjango-forms

Why am I getting Error 405 - Method not allowed when trying to save data from Django custom form?


Error 405 - Method not allowed when trying to save a data to a custom form using Django.

Method Not Allowed (POST): /create-establishment/ Method Not Allowed: /create-establishment/

I created a class, and the associated views and URL patterns. The HTML displays fine, and I can see the data in the Request. However, it just won't go through.

Model:

class Establishments(models.Model):
    org_name = models.CharField(max_length=255)
    address = models.TextField()
    epfo_code = models.CharField(max_length=20)
    esic_code = models.CharField(max_length=15)
    employer_name = models.CharField(max_length=60)
    employer_phone = models.CharField(max_length=10)
    contact_name = models.CharField(max_length=60)
    contact_phone = models.CharField(max_length=10)
    epfo_username = models.CharField(max_length=30)
    epfo_password = models.CharField(max_length=30)
    esic_username = models.CharField(max_length=30)
    esic_password = models.CharField(max_length=30)
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = "Establishments"

    def __str__(self):
        return self.org_name

Views:

``class CreateEstablishmentView(TemplateView):
    def get(self, request):
        form = CustomEstablishmentRegistrationForm()
        return render(request, 'create_establishment.html', {'form': form})

class EstablishmentDetailView(TemplateView):
    template_name = "establishment_detail.html"

def post(self, request):
    form = CustomEstablishmentRegistrationForm(request.POST)
    if form.is_valid():
        establishment = form.save()
        return redirect('establishment_detail', pk=establishment.pk)
    return render(request, 'create_establishment.html', {'form': form})

def establishment_detail(request, pk):
    establishment = Establishments.objects.get(pk=pk)
    return render(request, 'establishment_detail.html', {'establishment': establishment})`
`

URL:

    path("create-establishment/", views.CreateEstablishmentView.as_view(), name="create-establishment"),
    path('establishment-detail/<int:pk>/', views.establishment_detail, name='establishment_detail'),

The code should enter the data in the Establishments table.

I have tried including both POST and GET methods, but the error persists. I initially used the POST method to extract the data from the forms, but it looked clumsy, and didn't even work. Additionally, I tried using a function-based approach, but my limited knowledge in Django wasn't taking me anywhere.

The table works just fine, and I see it created in the DB.

def save_establishment(org_name, address, epfo_code, esic_code, employer_name, employer_phone, contact_name, contact_phone, epfo_username, epfo_password, esic_username, esic_password):
    establishment = Establishments(
        org_name=org_name,
        address=address,
        epfo_code=epfo_code,
        esic_code=esic_code,
        employer_name=employer_name,
        employer_phone=employer_phone,
        contact_name=contact_name,
        contact_phone=contact_phone,
        epfo_username=epfo_username,
        epfo_password=epfo_password,
        esic_username=esic_username,
        esic_password=esic_password
    )    
    establishment.save()  

Solution

  • It looks like your form is POSTing to the URL for 'CreateEstablishmentView'. However, that view doesn't have a POST method, hence the error.

    It looks like all you need to do is shift the POST method underneath the CreateEstablishmentView class, so the POST method is a method for that class.