I can't find the cause for a 405 error in my POST method

I am working on a form using the Django framework and I keep getting a 405 error which I can't seem to fix. I have an html template which contains a form allowing admins to add new TAs to the database

accountAdd.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Courses</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 2rem;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 600px;
            margin: auto;
            background: #fff;
            padding: 2rem;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            margin-bottom: 1.5rem;
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: bold;
        }
        select {
            width: 100%;
            padding: 0.5rem;
            margin-bottom: 1rem;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .button-group {
            display: flex;
            justify-content: space-between;
        }
        button {
            background-color: #333;
            color: #fff;
            padding: 0.5rem 1.5rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #555;
        }
    </style>
</head>
<body>
<center>
    <div class="container">
        <h1>Add Account</h1>
        {% if submitted%}
            TA added succesfully
        {% else %}
            <form action ="" method=POST>
                {% csrf_token %}

                {{form.as_p}}

                <input type="submit" value="Submit">
            </form>
        {% endif %}

    </div>
    </center>
</body>
</html>

Views.py (relevant method and imports only)

from django.shortcuts import render
from django.views import View
from .models import *
from .forms import  *
from django.http import HttpResponseRedirect

class accountAdd(View):
    def get(self,request):
        submitted = False

        if request.method == 'POST':
            form = TAForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('accountAdd?submitted=True')

        else:
            form = TAForm
            if 'submitted' in request.GET:
                submitted = True

        form = TAForm
        return render(request,'accountAdd.html', {'form': form, 'submitted': submitted})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('directory', views.directory.as_view(), name='directory'),
    path("courseDetails", views.courseDetails.as_view(), name='courseDetails'),
    path("courseEdit", views.courseEdit.as_view(), name='courseEdit'),
    path("", views.login.as_view(), name='login'),
    path("accountEdit", views.accountEdit.as_view(), name='accountEdit'),
    path('courseDetails/<str:courseID>', views.courseDetails.as_view(), name='courseDetails' ),
    path("accountAdd", views.accountAdd.as_view(), name='accountAdd'),
]

TA model in models.py

class TA(models.Model):
    name = models.CharField(max_length=100)
    phoneNum = models.CharField(max_length = 10)
    email = models.CharField(max_length = 100)
    officeHours = models.CharField(max_length = 100)
    password = models.CharField(max_length = 100, default = "pass")

What could be causing this issue? I am using sql-lite which automatically adds ids to the tables, could this have something to do with the issue?

from django.shortcuts import render
from django.views import View
from django.http import HttpResponseRedirect
from .forms import TAForm

class AccountAdd(View):
    def get(self, request):
        form = TAForm()
        submitted = request.GET.get('submitted') == 'True'
        return render(request, 'accountAdd.html', {'form': form, 'submitted': submitted})

    def post(self, request):
        form = TAForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accountAdd?submitted=True')
        return render(request, 'accountAdd.html', {'form': form, 'submitted': False})

Try the above code in views.py. Because 405 error happens for method not allowed.In your views.py you have written code for get method.But from browser you made POST request.Thats why 405 happens.The get method in views.py is called for only GET request,not POST request.

It doesn't make much sense to use a simple View in this case, since the logic your view has, is a typically CreateView pattern:

from django.views.generic import CreateView


class AccountCreateView(CreateView):
    form_class = TAForm
    template_name = 'accountAdd.html'

    def get_context_data(self, *args, **kwargs):
        return super().get_context_data(
            *args, **kwargs, submitted='submitted' in request.GET
        )

    def get_success_url(self):
        return '/accountAdd?submitted=True'

Usually you however reverse URLs, instead of constructing one as a string literal or with string interpolation.


Note: In Django, class-based views (CBV) often have a …View suffix, to avoid a clash with the model names. Therefore you might consider renaming the view class to AccountCreateView, instead of accountAdd.


Note: Django's templates are usually given names in snake_case, so account_add.html instead of accountAdd.html.

Back to Top