A view is returning a GET response when I am expecting a POST.

I am expecting a POST response with a queryset dictionary object, but I am instead receiving a GET response. I have tried looking through my urls and view files but I can’t find the mistake.
the view in question is createRoom. I put the print statement to confirm that the request.method != POST.

my views.py

from django.shortcuts import render, redirect
from . models import Room
from . forms import RoomForm

# Create your views here.




def home(request):
    rooms = Room.objects.all()
    context = {'rooms':rooms}
    return render(request, "base/home.html", context)

def room(request, pk):
    room = Room.objects.get(id=pk)
    context = {"room":room}
    return render(request, "base/room.html", context)

def createRoom(request):
    form = RoomForm()

    if request.method == "POST":
        form = RoomForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect("home")
    else:
        print("Request wasn't POST")

    context = {'form':form}
    return render(request, 'base/room_form.html', context)

Here is my app urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
    path("room/<str:pk>/", views.room, name="room"),
    path("create-room/", views.createRoom, name="create-room"),
]

my home.html file

{% extends 'main.html' %}

{% block content %}

<h1>Home Template</h1>

<div> 

    <a href="{% url 'create-room' %}"> Create Room</a>

    <div>
        {% for room in rooms %}
            <div>
                <span>@{{room.host.username}}</span>
                <h5>{{room.id}} -- <a href="{% url 'room' room.id %}">{{room.name}}</a></h5>
                <small>{{room.topic.name}}</small>
                <hr>
            </div>
        {% endfor %}
    </div>
</div>

{% endblock content %}

my room_form.html file


{% block content %}

<div>
    <form>
        {% csrf_token %}
        {{form.as_p}}
        <input type="submit" value="Submit">
    </form>
</div>

{% endblock content %}

Please let me know if I need to provide any more information

Your first request to that URL is going to be a GET to get the form.

What is making you think that your Submit is submitting via GET and not POST?

(For clarity, the GET and POSTs are referring to the http request submitted by the browser, they have nothing directly to do with the response.)

OK, so I understand when I click the create room link It sends a GET request to retrieve the form.

When I submit the form I expect the following

  1. redirect to home page
  2. display new topic/room link on home page.

Neither of these things are happening.

This is my terminal output when I submit my form

"Request wasn't POST"
[20/Dec/2022 20:59:18] "GET /create-room/?csrfmiddlewaretoken=3iYWpshOZZQAcrNxwAWx5VZbFnQ3lPSiZpZ5QEt408mHPPHRv4UYLxFhwDmC2XH8&host=2&topic=2&name=HELP%21&description= HTTP/1.1" 200 1490

I learned something new today.

From the docs at HTMLFormElement.method - Web APIs | MDN

Unless explicitly specified, the default method is ‘get’.

You don’t have a method="POST" attribute on your form.

Thanks again for your help. It works now. I wonder why the default is get when submitting a form :confused:

I can’t answer the “why”, but I was curious enough to do some digging. The first RFC I can find for specifying GET as the default was for HTML 3.2, first draft published in 1996. (Earlier standards identify the methods GET and POST as being available, but do not define either as a default.)

Given the state of equipment and networking available at the time, and that bandwidth and data transfer sizes being much more a significant concern, it makes sense to me that the default would be the more optimal choice. But that’s pure conjecture. (I’m not interested enough to try and dig through mailing lists to find specific references for that clause in the specs.)