2016-07-05 100 views
-1

我創建了兩種不同類型的用戶 - 卡車&公司使用Django。這裏是我的用戶註冊頁面Registration Page如何在django登錄後將不同類型的頁面重定向到不同的用戶

註冊後,關於用戶是卡車還是公司的數據將進入數據庫。

在我的登錄頁面中,只輸入EmailID和密碼。

我想知道具有唯一EmailID的用戶如何根據用戶類型重定向到有效頁面。

+0

你已經嘗試 – burning

+0

都能跟得上的任何一段代碼,我的天堂」 t試過任何東西。我是Django的新手。我剛剛創建了註冊頁面和登錄頁面,然後卡住了。沒有想法進一步移動。我可以分享我的CustomUser創建的詳細信息嗎? – vanam

回答

0

你需要類似的東西;

def user_login(request): 
    if request.method == 'POST': 
     form = AuthenticationForm(data=request.POST) 
     if form.is_valid(): 
      form.clean() 
      login(request, form.user_cache) 
      if form.user_cache.type == 'truck': 
       return HttpResponseRedirect('/some/where') 
      elif form.user_cache.type == 'company': 
       return HttpResponseRedirect('/some/where/else') 
    else: 
     form = AuthenticationForm() 

    return render(request, 'login.html', {'form' : form}) 
+0

謝謝你的回覆。我沒有在我的forms.py中創建一個AuthenticationForm。我剛剛創建了一個CustomRegistrationForm,以便用戶詳細信息可以保存到其他位置。但是我能夠將用戶詳細信息提供給我的views.py。我可以知道我該怎麼做。 – vanam

+0

其實AuthenticationForm來自django.contrib.auth.forms''''。我不需要複雜的身份驗證,所以我選擇了Django。 – Bestasttung

0

您定製的用戶模型可能是這樣的:

class myuser(models.Model): 
    myUser = models.ForeignKey(User,..) 
    userType = models.CharField(choices=(('truck','truck'),('company','company')) 

你的觀點應該是這樣的:

def login(request): 
# authentication and getting the data from POST request to your login page, 
# i assume you have a variable called user and you have your user object in it 
    userType = user.userType 
    if userType == company: 
     return HttpResponseRedirect('/some/url/') 
    if userType == truck: 
     return HttpResponseRedirect('/some/other/url/') 
相關問題