页面未找到显示(django)

时间:2017-05-10 18:44:35

标签: python django django-views django-urls

我的主页是登录页面。首先它将转到家庭功能,如果没有登录,它将转到login_user功能,然后登录后将重定向到家。但是找不到页面。这是我的代码。

views.py

def login_user(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('/')
            else:
                return render(request, 'retest/login.html', {'error_message': 'Your account has been disabled'})
    else:
        return render(request, 'retest/login.html', {'error_message': 'Invalid login'})
    return render(request, 'retest/login.html')

@login_required
def home(request):
    user = request.user
    if user.groups.filter(name='hod').exists():
         return render(request, 'retest/hod.html', {})
    elif user.groups.filter(name='principal').exists():
         return render(request, 'retest/principal.html', {})
    elif user.groups.filter(name='Rep').exists():
         return render(request, 'retest/home.html', {})
    elif user.groups.filter(name='Ajithzen').exists():
         return render(request, 'event/ajithzen.html', {})
    elif user.groups.filter(name='graphics').exists():
         return render(request, 'event/ashok.html', {}) 
    elif user.groups.filter(name='Event_incharge').exists():
         return render(request, 'event/chair.html', {})
    elif user.groups.filter(name='IEEE').exists():
         return render(request, 'event/ieee.html', {})
    else:
         return redirect('/login')

urls.py

url(r'login^$', views.login_user, name='login_user'),
url(r'^$', views.home, name='home'),
  

找不到页面(404)

     

请求方法:GET

     

请求网址:http://127.0.0.1:8000/accounts/login/?next=/

2 个答案:

答案 0 :(得分:2)

执行此操作而不是login^$

^login$

答案 1 :(得分:0)

默认情况下,@login_required 会将未经身份验证的用户重定向到 settings.LOGIN_URL
settings.LOGIN_URL 设置为 /accounts/login 作为默认值。 See the documentation.

因此,您的登录网址是/login,那么您需要在setting.py文件中设置LOGIN_URL

在setting.py末尾添加这一行

LOGIN_URL = "/login"

此外,您可能还想看看 LOGIN_REDIRECT_URL attribute