Django: GET /getMessages// HTTP/1.1" 404 2987
I am following a Django tutorial video on how to create a simple chat room. When I want to create a new room, a pop-up alert says "An error occurred". Couldn't figure out what went wrong.
Errors:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('<str:room>/', views.room, name='room'),
path('checkview', views.checkview, name='checkview')
> ]
views.py
from django.shortcuts import render, redirect
from chat.models import Room, Message
# Create your views here.
def home(request):
return render(request, 'home.html')
def room(request, room):
return render(request, 'room.html')
def checkview(request):
room = request.POST['room_name']
username = request.POST['username']
if Room.objects.filter(name=room).exists():
return redirect('/'+room+'/?username='+username)
else:
new_room = Room.objects.create(name=room)
new_room.save()
return redirect('/'+room+'/?username='+username)
models.py
from django.db import models
from datetime import datetime
# Create your models here.
class Room(models.Model):
name = models.CharField(max_length=1000)
class Message(models.Model):
value = models.CharField(max_length=1000000)
date = models.DateTimeField(default=datetime.now, blank=True)
user = models.CharField(max_length=1000000)
room = models.CharField(max_length=1000000)
- check the form in your template html file to make sure the form tag is added correctly. For example, it should look like this:
<form method="POST" action="/checkview"> {% csrf_token %} ... </form>
- make change in urls:
path('checkview/', views.checkview, name='checkview'),