2010-03-18 42 views
0

我有一個要求,我必須先通過電子郵件註冊用戶。所以,我用django-registraton去了,我設法將tat模塊整合到我的django項目中。 登錄成功後,頁面重定向到'registration/profile.html'。 我需要訪問用於驗證的用戶對象。 我需要此對象來更改模型,該模型保存有關我的用戶的自定義配置文件信息。我已經在我的models.py配置文件頁面獲取用戶對象在Django

這裏是我用來重新指向我的模板的URL .. url(r'^ profile/$',direct_to_template,{'template':'registration /profile.html'}),

所以我的問題是這樣的...登錄後,用戶必須帶到需要填寫的個人資料頁面。 有關我如何實現這一點的任何想法?

回答

1

我已經設置了類似的東西。在我的情況下,我通過管理界面定義了新用戶,但基本問題是一樣的。我需要在第一次登錄時顯示特定頁面(即用戶設置)。

我最終在UserProfile模型中添加了一個標誌(first_log_in,BooleanField)。我在處理路由的首頁的視圖函數中設置了一個檢查。這是粗糙的想法。

views.py:

def get_user_profile(request): 
    # this creates user profile and attaches it to an user 
    # if one is not found already 
    try: 
     user_profile = request.user.get_profile() 
    except: 
     user_profile = UserProfile(user=request.user) 
     user_profile.save() 

    return user_profile 

# route from your urls.py to this view function! rename if needed 
def frontpage(request): 
    # just some auth stuff. it's probably nicer to handle this elsewhere 
    # (use decorator or some other solution :)) 
    if not request.user.is_authenticated(): 
     return HttpResponseRedirect('/login/') 

    user_profile = get_user_profile(request) 

    if user_profile.first_log_in: 
     user_profile.first_log_in = False 
     user_profile.save() 

     return HttpResponseRedirect('/profile/') 

    return HttpResponseRedirect('/frontpage'') 

models.py:

from django.db import models 

class UserProfile(models.Model): 
    first_log_in = models.BooleanField(default=True, editable=False) 
    ... # add the rest of your user settings here 

您在您的setting.py設置AUTH_PROFILE_MODULE指向模型是很重要的。 IE瀏覽器。

AUTH_PROFILE_MODULE = 'your_app.UserProfile' 

應該工作。

查看this article以獲取有關UserProfile的進一步參考。我希望有所幫助。 :)

+0

所以你的建議是將這些方法添加到自定義UserOption模型是嗎?並更改窗體的動作參數來調用例如frontpage rgt? 對於django來說有點新鮮..所以如果你能詳細闡述.. will be useful .. – Sharath

+0

我擴大了答案。希望現在更清楚。 :) –

+0

對不起..錯過views.py標題...:P 非常感謝..你一直是最有幫助的.. – Sharath

相關問題