2013-02-25 123 views
3

我使用django 1.5与自定义模型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个人资料?谢谢!

+0

您是否将应用程序放入您安装的应用程序并同步? – catherine 2013-02-25 14:18:21

回答

0

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

+0

你是什么意思“申请中的班级”?我的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', '应用', ) AUTH_USER_MODEL = 'app.MyUser' – Wolter 2013-02-25 13:28:24

+0

@Wolter我的意思是类MYUSER应该在 “应用程序” 的 – almalki 2013-02-25 14:12:27

+0

models.py我的项目中只有一个model.py。 – Wolter 2013-02-25 14:28:05

相关问题