Helo everyone, I have two views but I can't redirect to view from another view. Before asking this question I tried some solutions from stackoverflow, but they they didn't bring any result. Please help.
VIEWS.py
def new_room(request):
new_room = None
while not new_room:
with transaction.atomic():
label = haikunator.haikunate()
if Room.objects.filter(label=label).exists():
continue
new_room = Room.objects.create(label=label)
return redirect('chat', label=label)
def chat(request, label):
room, created = Room.objects.get_or_create(label=label)
messages = reversed(room.messages.order_by('-timestamp')[:50])
return render(request, "chat/room.html", {
'room': room,
'messages': messages,
})
URLS.PY
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/$', views.chat, name='room'),
]
Thanks in advance!
Your call to redirect
looks like it's using the name of a function rather than the name of a named view. We would need to see your urls.py to be sure.
Remember, redirect
uses reverse
under the hood - you'll need to supply the same name you use in your URL patterns, as well as any necessary namespace. If you can post your existing urls.py files I can try to provide more concrete information on how to fix your problem. In the meantime review the examples of how to use redirect
here: https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#examples
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments