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