我有一个自定义注册表单供我的用户在我的应用上添加个人资料.但是,最近出现了一个错误,即表单没有保存放入所有字段的信息.
我的用户模型MyUser与另一个模型有一个ManyToMany关系Interest,这就是问题出现的地方.我不确定它是否是导致它RegistrationForm的register视图,因此我在下面列出了以及模型代码.我还有一个视图,用户可以在创建后更新他们的个人资料,这也非常完美.这是personal观点.正如我所说的Interest那样,即使在注册页面上填写了该字段,也只是该字段未被返回.
非常感谢任何帮助或建议,谢谢.
models.py
class Interest(models.Model):
title = models.TextField()
def __unicode__(self):
return self.title
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
date_of_birth = models.DateField()
course = models.ForeignKey(Course, null=True)
location = models.ForeignKey(Location, null=True)
interests = models.ManyToManyField(Interest, null=True)
bio = models.TextField(blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
Run Code Online (Sandbox Code Playgroud)
views.py
def register(request):
if request.method == …Run Code Online (Sandbox Code Playgroud) django django-templates django-models django-forms django-views
我正在创建一个使用自定义用户模型的应用程序(使用该AbstractBaseUser方法).我已经设法编写了所有相关的用户创建表单,并为用户提供了登录的方式.但是,当我尝试创建一个需要当前登录用户信息的页面时(例如编辑帐户页面)我是遇到麻烦.我尝试了各种不同的方法,request.user但似乎都没有.当我尝试使用此方法时,我得到TypeError - "'MyUser'对象不支持索引".
我附上我的模型类作为参考.
任何帮助将不胜感激,提前感谢.
models.py
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, 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, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
u = self.create_user(email=email,
password=password,
date_of_birth=date_of_birth
) …Run Code Online (Sandbox Code Playgroud) 我有一个包含四个不同位置的String,用逗号分隔.我将长字符串拆分为四个部分并将位置分配给location变量.目前这很简单(见下文).
int noOfLocations = counter + 1;
//Splits the long input location string into the four locations.
String[] locations = inputLocation.split(",");
String location1 = locations[0];
String location2 = locations[1];
String location3 = locations[2];
String location4 = locations[3];
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够输入任意数量的位置,并为每个使用for循环分配一个变量.
我觉得这是一件相当简单的事情,但我似乎无法正确编码.我有一个计数器,它计算逗号的数量,并在该数字上加一个以查找位置数.
基本上我想说的是:
String[] locations = inputLocation.split(",");
for i in range noOfLocations {
String location(i) = locations[i];
}
Run Code Online (Sandbox Code Playgroud)
写这样的东西的正确语法是什么?
django ×2
django-admin ×1
django-forms ×1
django-users ×1
django-views ×1
for-loop ×1
java ×1
loops ×1
python ×1
string ×1