Httpresponse重定向到基于类的视图

时间:2014-09-20 09:04:39

标签: python django

由于某些原因,我无法使用django的默认身份验证系统,因为我编写了自定义登录功能。现在每次成功登录我希望我的用户重定向到另一个基于应用程序类的视图。

即:

#frontend/views.py
login(request, user)
return HttpResponseRedirect(reverse('platform:UserProfileView'))

#frontend/urls.py
urlpatterns = patterns('frontend.views',
    url(r'^$', 'base_view', name='base_view'),
    url(r'^accounts/login/$', 'login_user', name='login_user')
)

#platform/urls.py
urlpatterns = patterns('platform.views',
    url(r'^(?P<user>.+)$', UserProfileView.as_view(), name='user_profile_view'),
)

现在我的上述解决方法给出了以下错误...

  

反向'UserProfileView',找不到参数'()'和关键字参数'{}'。尝试了0种模式:[]

#platform/vies.py

class UserProfileView(View):
    @method_decorator(login_required)
    def get(self, request, user):
        if request.user.username == user:
            profile = request.user
            return render(request, 'app/homepage.html', {'profile': profile})
        else:
            raise Http404

我错过了什么......?或者其他任何方式?

完整追溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/login/

Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'frontend',
 'platform')
Installed Middleware:
('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/vijay/nightybuild/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vijay/nightybuild/readtamil/frontend/views.py" in login_user
  49.                 return HttpResponseRedirect(reverse('UserProfileView', args=(user.username,)))
File "/home/vijay/nightybuild/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  546.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/vijay/nightybuild/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  463.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /accounts/login/
Exception Value: Reverse for 'UserProfileView' with arguments '(u'username',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:0)

您为网址定义了名称。使用带有适当参数的名称。

from django.core.urlresolvers import reverse

...

login(request, user)
return HttpResponseRedirect(reverse('platform:user_profile_view',
                                    args=(user.username,)))

# OR

return HttpResponseRedirect(reverse('platform:user_profile_view',
                                    kwargs={'user': user.username}))

注意:我假设视图接受用户名作为参数。相应地更改args