Part 2: Templates
============================
We will edit the views, urls and templates for posting a Room form, and joining it.
We will edit the ``index.html`` file, for posting a new room.
.. code-block:: html
Chat Rooms
What chat room would you like to enter?
Next, edit ``urls.py``.
.. code-block:: python
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('room//', views.room, name='room'),
]
Editing existing views
----------------------
We will edit the ``views.py``
.. code-block:: python
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render, reverse
from .models import Room
def index(request):
if request.method == "POST":
name = request.POST.get("name", None)
if name:
try:
room = Room.manager.get(name=name)
return HttpResponseRedirect(reverse("room", args=[room.pk]))
except Room.DoesNotExist:
pass
room = Room.objects.create(name=name, host=request.user)
return HttpResponseRedirect(reverse("room", args=[room.pk]))
return render(request, 'chat/index.html')
def room(request, pk):
room: Room = get_object_or_404(Room, pk=pk)
return render(request, 'chat/room.html', {
"room":room,
})
We need to create or update our `room.html` template:
.. code-block:: html
Chat Room
With this created w should now be able to create a room and enter it.