0

代码是

#urls.py

from django.conf.urls import patterns, url
from employees import views
from schdeules import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout'), 
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name':    'admin/login.html'}), 
url(r'^accounts/$', 'django.views.generic.simple.redirect_to', {'url': '/'}), 
url(r'^accounts/profile/$', 'django.views.generic.simple.redirect_to', {'url': '/'}),
)

#views.py 
# Create your views here.
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def home(request):
  welecome = 'hai welcome to opas'
  context = {'temp_var': welecome}
  return render(request, 'schdeules/home.html')

and iam getting an error

Exception Type: ViewDoesNotExist at /opas/
Exception Value: Could not import django.views.generic.simple.redirect_to. Parent module   django.views.generic.simple does not exist.

我想使用 django 默认登录和注销模块。如果成功登录,那么我想重定向到主页。提前致谢。

4

1 回答 1

0

基于函数的通用视图redirect_to在 Django 1.3 中被弃用,并在 Django 1.5 中被删除。请改用基于类的通用视图RedirectView

from django.views.generic.base import RedirectView

urlpaterns = ('',
...
    url(r'^accounts/$', RedirectView.as_view(url='/')), 
    url(r'^accounts/profile/$', RedirectView.as_view(url='/')),
)

请注意,您不必包含 和 的网址/accounts/格式/accounts/profile/。您可能最好在设置LOGIN_REDIRECT_VIEW中进行设置,以便用户在登录后直接重定向到主页。

LOGIN_REDIRECT_VIEW = 'home' # using a named url pattern requires Django 1.5 or later
于 2013-10-31T10:40:29.137 回答