2012-01-28 154 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

相关问题