姜戈 1.5。用户配置文件创建
Posted
技术标签:
【中文标题】姜戈 1.5。用户配置文件创建【英文标题】:Django 1.5. Users profile creation 【发布时间】:2013-02-10 14:42:58 【问题描述】:我使用带有自定义模型 MyUser 的 django 1.5。我想制作用户个人资料页面,他只能修改一个字段 - “关于”。 我尝试了类似的方法:
forms.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 中创建用户个人资料?谢谢!
【问题讨论】:
您是否将应用放入已安装的应用中并同步? 【参考方案1】:MyUser
类应位于设置文件中名为 'app'
的应用程序下,以便身份验证框架选择它。
【讨论】:
什么是“申请中的班级”?我的 settings.py: INSTALLED_APPS = ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_admin_bootstrapped ', 'django.contrib.admin','app', ) AUTH_USER_MODEL = 'app.MyUser' @Wolter 我的意思是 MyUser 类应该在“app”的 models.py 中 我的项目中只有一个model.py。 @Wolter 我怀疑 UserSettingsForm 的 get_user_model() 引起了问题,请尝试使用 MyUser。以上是关于姜戈 1.5。用户配置文件创建的主要内容,如果未能解决你的问题,请参考以下文章