0
我從下面的觀點得到NoReverseMatch
:Django的shortcuts.redirect NoReverseMatch
from django.shortcuts import render, redirect
def new_room(request):
label="test"
return redirect(chat_room, label=label)
# this didn't work either:
# return redirect('chat_room', label=label)
def chat_room(request, label):
...
我urls.py看起來是這樣的:
from django.conf.urls import url, include
from messaging import views
app_name="messaging"
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/', views.chat_room, name='chat_room'),
]
直去messaging/test/
將加載頁面正確。這只是導致問題的重定向。
完整堆棧跟蹤:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/messages/new/
Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
['messaging',
'dal',
'dal_select2',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',,
'channels']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "g:\Python\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "g:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "g:\Python\lib\site-packages\channels\handler.py" in process_exception_by_middleware
240. return super(AsgiHandler, self).process_exception_by_middleware(exception, request)
File "g:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "g:\Git\Jam\messaging\views.py" in new_room
26. return redirect('chat_room', label=label)
File "g:\Python\lib\site-packages\django\shortcuts.py" in redirect
56. return redirect_class(resolve_url(to, *args, **kwargs))
File "g:\Python\lib\site-packages\django\shortcuts.py" in resolve_url
147. return reverse(to, args=args, kwargs=kwargs)
File "g:\Python\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "g:\Python\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /messages/new/
Exception Value: Reverse for 'chat_room' with arguments '()' and keyword arguments '{'label': 'test'}' not found. 0 pattern(s) tried: []