我想根据django中的登录凭据重定向到不同的页面。
举个例子:提供了两个登录
如果我使用X凭据登录,那么我将被重定向到X页面。在相同的登录页面上,如果我使用Y凭据登录,我将被重定向到相应的Y页面。
我试图将条件放入user_login views.py文件中,但这给了我以下错误。
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'companyDashboard.html' not found. 'companyDashboard.html' is not a valid view function or pattern name.
我的views.py文件
from django.shortcuts import render
from django.template import RequestContext
from wfhApp.forms import UserForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def company_dashboard(request):
return render(request, 'wfhApp/company_dashboard.html')
@login_required
def companyDashboard(request):
return render(request, 'wfhApp/companyDashboard.html')
@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username= username, password=password)
if user:
if user.is_active:
if user.username == 'prashant':
login(request, user)
return HttpResponseRedirect(reverse('company_dashboard.html'))
elif user.username == 'saurabh':
login(request, user)
return HttpResponseRedirect(reverse('companyDashboard.html'))
else:
return HttpResponse("Account Not Exists")
else:
return HttpResponse("Account Not Active")
else:
print("Someone tried to login and failed")
print("Username: {} and Password: {}".format(username, password))
return HttpResponse("Invalid Login Details")
else:
return render(request, 'wfhApp/login.html')
感谢您的时间和考虑。
查看urresolvers文档,若要使用reverse
,必须在路径(urls.py(中设置名称。不要添加.html
扩展名,请在urls.py
处使用设置的名称。
from news import views
path('archive/', views.archive, name='news-archive')
使用设置的名称。
# using the named URL
reverse('news-archive')
# passing a callable object
# (This is discouraged because you can't reverse namespaced views this way.)
from news import views
reverse(views.archive)