2012-01-28 46 views
2

我是Django的初學者。我需要設置一個網站,每個用戶都有一個個人資料頁面。我見過django管理員。用戶的配置文件頁面應該存儲一些只能由用戶編輯的信息。任何人都可以指出我怎麼可能?任何教程鏈接都會非常有幫助。另外,是否有任何django模塊,可用於設置用戶頁面。在Django中創建用戶個人資料頁面

+1

你已經開發了什麼? – Dean 2012-01-28 15:50:28

+0

我已經完成驗證。 – jvc 2012-01-28 16:02:23

+4

http://stackoverflow.com/questions/8177289/django-profiles http://stackoverflow.com/questions/7313336/django-multiple-user-profiles-subprofiles http://stackoverflow.com/questions/3100521/django-registration-and-multiple-profiles http://stackoverflow.com/questions/4171083/is-there-a-django-app-that-c​​an-manage-your-users-profiles http ://stackoverflow.com/questions/7173279/django-registration-and-django-profiles http://stackoverflow.com/questions/2654689/django-how-to-write-users-and-profiles-handling- in-best-way http://stackoverflow.com/questions/1910359/creating-a-extended-use – 2012-01-28 16:20:44

回答

8

如果用戶創建GET請求或在創建POST請求時更新用戶的個人資料數據,您只需創建一個可供已認證用戶使用的視圖並返回配置文件編輯表單。

大部分工作已完成,因爲編輯模型的編號有generic views,例如UpdateView。您需要擴展的是檢查經過身份驗證的用戶併爲其提供您想要提供編輯的對象。這是MTV黑社會中的視圖組件,它提供編輯用戶個人資料的行爲 - Profile模型將定義user profile,模板將以離散方式提供演示。

所以這裏的一些行爲,你扔一個簡單的解決方案:

from django.contrib.auth.decorators import login_required 
from django.views.generic.detail import SingleObjectMixin 
from django.views.generic import UpdateView 
from django.utils.decorators import method_decorator 

from myapp.models import Profile 


class ProfileObjectMixin(SingleObjectMixin): 
    """ 
    Provides views with the current user's profile. 
    """ 
    model = Profile 

    def get_object(self): 
     """Return's the current users profile.""" 
     try: 
      return self.request.user.get_profile() 
     except Profile.DoesNotExist: 
      raise NotImplemented(
       "What if the user doesn't have an associated profile?") 

    @method_decorator(login_required) 
    def dispatch(self, request, *args, **kwargs): 
     """Ensures that only authenticated users can access the view.""" 
     klass = ProfileObjectMixin 
     return super(klass, self).dispatch(request, *args, **kwargs) 


class ProfileUpdateView(ProfileObjectMixin, UpdateView): 
    """ 
    A view that displays a form for editing a user's profile. 

    Uses a form dynamically created for the `Profile` model and 
    the default model's update template. 
    """ 
    pass # That's All Folks! 
+1

對文檔字符串使用雙引號以正確語法突出顯示 – glarrain 2012-09-14 17:54:32

+0

謝謝,您的權利! – 2012-09-14 20:39:42

+0

感謝你 - 非常有用!有兩件事我不得不改變以使其工作:而不是'@ login_required',你需要'@method_decorator(login_required)'(從'django.utils.decorators'導入)。而不是使用'BaseUpdateView'使用'UpdateView',否則會出現'render_to_response'丟失的錯誤。 – 2012-09-19 11:14:08

1

可以

  • 創建用於存儲有關用戶
  • 輪廓信息的另一個模型添加AUTH_PROFILE_MODULE='yourprofileapp.ProfileModel'到的settings.py
  • 在配置文件編輯視圖中,只允許登錄的用戶編輯自己的配置文件

    例如:

    @login_required 
    def edit_profile(request): 
        ''' 
        edit profile of logged in user i.e request.user 
        ''' 
    
  • 您也可以確保每當新用戶創建用戶的配置文件還使用Django的信號

創建從Django文檔閱讀storing additional information about users