jvc*_*jvc 3 django django-models
我是Django的初学者.我需要设置一个网站,每个用户都有一个个人资料页面.我见过django admin.用户的个人资料页面应存储一些只能由用户编辑的信息.谁能指出我怎么可能?任何教程链接都会非常有用.此外,是否有任何django模块,可用于设置用户页面.
您只需要创建一个可供经过身份验证的用户使用的视图,并在创建GET请求时返回配置文件编辑表单,或者在创建请求时更新用户的配置文件数据POST.
大多数工作已经为您完成,因为有编辑模型的通用视图,例如UpdateView.扩展它需要的是检查经过身份验证的用户并为其提供要为其提供编辑的对象.这是MTV三元组中的视图组件,它提供了编辑用户配置文件的行为 - 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!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12256 次 |
| 最近记录: |