gpt4 book ai didi

Django 1.5。用户资料创建

转载 作者:行者123 更新时间:2023-12-02 05:02:28 28 4
gpt4 key购买 nike

我将 django 1.5 与自定义模型 MyUser 结合使用。我想制作用户个人资料页面,他只能在其中修改一个字段 - “关于”。我试过类似的东西:

表单.py:

class UserSettingsForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ('about')

View .py:

class UserSettings(UpdateView):
form_class = UserSettingsForm
template_name = "user/settings.html"
def get_object(self, queryset=None):
return self.request.user
def get_success_url(self):
return reverse('user_detail', args=[self.request.user.username])

网址:

url(r'^settings/$', UserSettings.as_view(), name='user_settings')

型号:

    class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')

user = self.model(
email=MyUserManager.normalize_email(email),
# date_of_birth=date_of_birth,
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(email,
password=password,
#date_of_birth=date_of_birth
)
user.is_admin = True
user.save(using=self._db)
return user


class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
db_index=True,
)
last_name=models.CharField(max_length=30)
first_name=models.CharField(max_length=30)
about=models.TextField(blank=True)

objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name','first_name','about']

def get_full_name(self):
# The user is identified by their email address
return self.email

def get_short_name(self):
# The user is identified by their email address
return self.email

def __unicode__(self):
return self.email

def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True

def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True

@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin

但我收到错误:django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL 指的是尚未安装的模型“app.MyUser”

如何在 Django 1.5 中创建用户配置文件?谢谢!

最佳答案

MyUser 类应该在您的设置文件中名为 'app' 的应用程序下,以便身份验证框架选择它。

关于 Django 1.5。用户资料创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067249/

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