2016-11-19 22 views
1

管理者用の「ダッシュボード」とログインフォームにリダイレクトして「管理パネル」にリダイレクトするにはどうすればいいですか?どのように私は、Djangoの異なる2つのログインフォームを作ることができますか?

私はこれを探して、ModelBackend参考としてこれを見つけましたが、それをどうやって行うのかはわかりません。 https://docs.djangoproject.com/en/1.8/topics/auth/customizing/

+0

次の2つのビューを作成することができます。最初のものは「ダッシュボード」に、2番目のパネルは「管理パネル」にリダイレクトされます。ログインフォームによって –

+0

は、あなたがDjangoのデフォルトのログイン・ページを使用したい意味ですか、およびログインが成功にはいくつかの他の着陸にあなたを再指示する必要があり? –

+0

しかし、私はそれをどのように行うことができますか?私は検索していましたが、私はModelBackendで見つけました。 –

答えて

1

私はあなたが2つのログインのビューを必要と理解しています。あなたのアプリでは、たとえば、2つのビューを作成するのviews.py:

def loginDashboard(request): 
    if request.user.is_authenticated(): 
    return HttpResponseRedirect('/') 
    if request.method == 'POST': 
    form = LoginForm(request.POST) 
    if form.is_valid(): 
     username = form.cleaned_data['username'] 
     password = form.cleaned_data['password'] 
     account = authenticate(username=username, password=password) 
     if account is not None: 
     login(request, account) 
#here is redirecting to dashboard 
      return HttpResponseRedirect('/dashboard/') 
     else: 
     return render(request, 'profiles/login.html', context) 
    else: 
     return render(request, 'profiles/login.html', context) 
    else: 
    form = LoginForm() 
    context = {'form':form} 
    return render(request, 'profiles/login.html', context) 

def loginAdminPanel(request): 
    if request.user.is_authenticated(): 
    return HttpResponseRedirect('/') 
    if request.method == 'POST': 
    form = LoginForm(request.POST) 
    if form.is_valid(): 
     username = form.cleaned_data['username'] 
     password = form.cleaned_data['password'] 
     account = authenticate(username=username, password=password) 
     if account is not None: 
     login(request, account) 
#here is redirecting to admin panel 
      return HttpResponseRedirect('/adminpanel/') 
     else: 
     return render(request, 'profiles/login.html', context) 
    else: 
     return render(request, 'profiles/login.html', context) 
    else: 
    form = LoginForm() 
    context = {'form':form} 
    return render(request, 'profiles/login.html', context) 

そして、あなたのurls.py:この場合

url(r'^login-dash/$', views.loginDashboard), 
url(r'^login-admin/$', views.loginAdminPanel), 

使用すると、2つのログインページ(example.com/login-dashexample.com/login-admin

を持っているがあなたのforms.py:

class LoginForm(forms.Form): 
username = forms.CharField(label=(u'Username')) 
password = forms.CharField(label=(u'Pasword'), widget=forms.PasswordInput(render_value=False)) 

希望します。

+0

私はこれをしようとしているが、いくつかの瞬間に、私はあなたを教え、そんなにありがとう –

関連する問題