如何在 Django 1.9 中获取用户配置文件
How to get users profile in django 1.9
我的 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 has no attribute profile"。
我什么都试过了,get_profile(), get_profile, profile(), profile... 什么都没用。谁能帮我?谢谢。
请注意 AUTH_PROFILE_MODULE 设置和 get_profile() 方法 where remove in django 1.7。
尝试将 related_name
添加到您的 MyUserProfile.user
字段。
另请注意,您可以只添加在用户模型上提交的 slug 并避免额外的关系。
我的 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 has no attribute profile"。 我什么都试过了,get_profile(), get_profile, profile(), profile... 什么都没用。谁能帮我?谢谢。
请注意 AUTH_PROFILE_MODULE 设置和 get_profile() 方法 where remove in django 1.7。
尝试将 related_name
添加到您的 MyUserProfile.user
字段。
另请注意,您可以只添加在用户模型上提交的 slug 并避免额外的关系。