gpt4 book ai didi

python - 如何在 django 1.9 中获取用户个人资料

转载 作者:太空宇宙 更新时间:2023-11-03 16:35:28 24 4
gpt4 key购买 nike

我的views.py文件中有这个方法

def guest_login(request):
form = SomeForm(request.POST or None)
if form.is_valid():
email = request.POST['email']
password= request.POST['password']
u = authenticate(username = email, password = password)
if uis not None:
if u.is_active:
login(request, u)
userProfile= request.user.profile
return HttpResponseRedirect(userProfile.get_absolute_url())
else:
messages.error(request, 'Text', extra_tags='text')
else:
messages.error(request, 'Text', extra_tags='text')
else:
form = SomeForm()
context = {
"form": form,
}

return render(request, 'loginpage.html', context)

所以,我没有扩展基本用户,我创建了自己的用户,我是这样做的:

class MyUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), max_length=255, db_index=True, blank = False, unique = True)
name= models.CharField(blank = False, max_length = 100)
surname= models.CharField(blank = False, max_length = 100)
# slug = models.SlugField(unique = True)
is_staff = models.BooleanField(
_('some_text'), default=False, help_text=_(
'text'))
is_active = models.BooleanField(_('ctive'), default=True, help_text=_(
'text'
'text'))
date_joined = models.DateTimeField(_('date created'), default=timezone.now)

objects = UserManager()

USERNAME_FIELD = 'email'

class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
# abstract = True

def get_full_name(self):
return self.name + " " + self.surname

def get_short_name(self):
return self.name

def get_email(self):
return self.email

def email_user(self, subject, message, from_email=None, **kwargs):
"""Send an email to this User."""
send_mail(subject, message, from_email, [self.email], **kwargs)

class MyUserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(unique=True)

def get_absolute_url(self):
return reverse("user:restoraunts", kwargs={"slug": self.slug})

User.profile = property(lambda u: MyUser.objects.get_or_create(user=u)[0])

def create_slug(instance, new_slug=None):
slug = slugify(instance.user.name)
if new_slug is not None:
slug = new_slug
qs = MyUserProfile.objects.filter(slug=slug).order_by('-id')
exists = qs.exists()
if exists:
new_slug = "%s-%s" % (slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug

def pre_save_guest_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
#
pre_save.connect(pre_save_guest_receiver, sender=MyUserProfile)

并在settings.py中添加:

AUTH_USER_MODEL = 'korisnik.Korisnik'
AUTH_PROFILE_MODULE = 'korisnik.KorisnikProfil'

一切正常,除了这一行:

userProfile= request.user.profile

它说“MyUser 没有属性配置文件”。我尝试了所有方法,get_profile()、get_profile、profile()、profile...没有任何效果。谁能帮我?谢谢。

最佳答案

请注意 AUTH_PROFILE_MODULE 设置和 get_profile() 方法 where remove in django 1.7 .

尝试将 lated_name 添加到您的 MyUserProfile.user 字段。

另请注意,您也许可以只在用户模型上添加该 slug 文件并避免额外的关系。

关于python - 如何在 django 1.9 中获取用户个人资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com