0

I got error and don't know how to fix it reverse for chat_room not found. chat_room is not a valid view function or pattern name

/home/mbae/.local/lib/python3.5/site-packages/channels/handler.py in process_exception_by_middleware

            return super(AsgiHandler, self).process_exception_by_middleware(exception, request)

 ...

▶ Local vars /home/mbae/.local/lib/python3.5/site-packages/django/core/handlers/base.py in _get_response

                response = wrapped_callback(request, *callback_args, **callback_kwargs)

 ...

▶ Local vars /home/mbae/465/mysite/prj/views.py in new_room

    return redirect(chat_room, label=label) 

 ...

▶ Local vars

views.py

def new_room(request):
    new_room = None
    while not new_room:
        with transaction.atomic():
            l = Haikunator()
            label=l.haikunate()
            if Room.objects.filter(label=label).exists():
                continue
            new_room = Room.objects.create(label=label)
    return redirect(chat_room, label=label)

def chat_room(request, label):

    room, created = Room.objects.get_or_create(label=label)

    messages = reversed(room.messages.order_by('-timestamp')[:50])

    return render(request, "prj/chat.html", {
        'room': room,
        'messages': messages,
})

urls.py

url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/$', views.chat_room, name='chat_room'),

room.html:

<h1>{{ room.label }}</h1>
<p class="quiet">
    Anyone with this URL can join the room and chat:
    <code>{{ request.scheme }}://{{ request.get_host }}/{{ room.label }}</code>
</p>
<p>
    <label for="handle">Your name:</label>
    <input id="handle" type="text" placeholder="handle">
</p>
<form id="chatform">
    <table id="chat">
        <tbody>
            {% for message in messages %}
                <tr>
                    <td>{{ message.formatted_timestamp }}</td>
                    <td>{{ message.handle }}</td>
                    <td>{{ message.message }}</td>
                </tr>
            {% endfor %}
        </tbody>
        <tfoot>
        <tr>
            <td>Say something:</td>
            <td colspan=2>
                <input id="message" type="text" placeholder="message">
                <button type="submit" id="go">Say it</button>
            </td>
        </tfoot>
    </table>
</form>
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18

1 Answers1

0

I think this is the problem line:

return redirect(chat_room, label=label)

Try passing "chat_room" instead of chat_room!

See also: django redirect to view

Josiah Krutz
  • 957
  • 6
  • 16