码迷,mamicode.com
首页 > 其他好文 > 详细

django扩展User模型(model),profile

时间:2018-09-06 18:10:50      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:htm   blank   val   form   file   turn   from   load   efi   

from django.contrib.auth.models import User


# Create your models here.


class Profile(models.Model):
    user = models.OneToOneField(User, related_name=profile)
    date_of_birth = models.DateTimeField(blank=True, null=True)
    photo = models.ImageField(upload_to=user/%Y/%m/%d, blank=True)

    def __str__(self):
        return Profile for user {}.format(self.user.username)

修改setting.py

AUTH_PROFILE_MODULE = account.Profile‘  # app名.model名

按照下面方式调用

@login_required
def edit(request):
    if request.method == POST:
        user_form = UserEditForm(instance=request.user,
                                 data=request.POST)
        profile_form = ProfileEditForm(instance=request.user.profile,
                                       data=request.POST,
                                       files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)
    return render(request,
                  account/edit.html,
                  {user_form: user_form,
                   profile_form: profile_form})

 

django扩展User模型(model),profile

标签:htm   blank   val   form   file   turn   from   load   efi   

原文地址:https://www.cnblogs.com/gange111/p/9599228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!