我的项目包含一些应用,例如帐户, Webadmin ,等
登录后我将用户重定向到 Webadmin 的视图。 事情是我可以通过浏览器查看 Webadmin 的页面!这意味着链接正在运行。
但每当我尝试重定向查看时,它都会给我这个错误。这是追溯
Environment:
Request Method: POST
Request URL: http://localhost:8000/account/login/
Django Version: 1.7
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar.apps.DebugToolbarConfig',
'bootstrap3',
'easy_pjax',
'website',
'account',
'webadmin')
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/nihan/venv/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/nihan/workspace/hubx/account/views.py" in sign_in
57. return redirect(webadmin_home)
File "/home/nihan/venv/venv/lib/python3.4/site-packages/django/shortcuts.py" in redirect
74. return redirect_class(resolve_url(to, *args, **kwargs))
File "/home/nihan/venv/venv/lib/python3.4/site-packages/django/shortcuts.py" in resolve_url
158. return urlresolvers.reverse(to, args=args, kwargs=kwargs)
File "/home/nihan/venv/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in reverse
546. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/nihan/venv/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
463. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /account/login/
Exception Value: Reverse for 'webadmin.views.list_sites' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
以下是来自网站应用(根应用)
的urls.py代码from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns(
'',
url(r'^$', 'website.views.home', name='home'),
url(r'^account/', include('account.urls', namespace='account')),
url(r'^admin/', include('webadmin.urls', namespace='webadmin')),
url(r'^admin-panel/', include(admin.site.urls)),
)
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
来自 webadmin 应用的url.py代码
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'create/$', 'webadmin.views.create', name='create'),
url(r'list/$', 'webadmin.views.list_sites', name='list_sites'),
)
来自帐户应用的url.py代码
from django.conf.urls import patterns, url
urlpatterns = patterns( '',
url(r'^view/(?P<id>\d+)/$', 'account.views.view', name='profile'),
url(r'^login/$', 'account.views.sign_in', name='sign_in'),
url(r'^register/$', 'account.views.sign_up', name='sign_up'),
url(r'^logout/$', 'account.views.signout', name='logout'),
url(r'^edit/$', 'account.views.edit', name='edit'),
url(r'^activate/(?P<token>\d{4})/$', 'account.views.activate', name='activate'),
)
我使用这个小代码重定向到 list_sites (与其他应用的其他视图一起使用)
from django.shortcuts import redirect
from webadmin.views import list_create as webadmin_home
def someview(request):
return redirect(webadmin_home)
我错过了什么吗?为什么这个错误会被or?怎么解决? 提前致谢
答案 0 :(得分:4)
您不会重定向到视图方法,而是重定向到网址。在您的urls.py中,您已为该URL指定了名称,因此您需要使用该名称。
return redirect('webadmin:create')
答案 1 :(得分:0)
它有助于查看&#39; account.urls&#39;提交你在websiteApp / urls.py中引用的文件,但听起来好像它没有找到&#34; account / urls.py&#34;文件本身,或者您的&#34;登录&#34;该文件中的视图不正确。
答案 2 :(得分:0)
from django.core.urlresolvers import reverse_lazy
return reverse_lazy("webadmin:list_create")