关于 Django 和用户身份验证的问题
Django 的菜鸟问题:
我使用 dango.contrib.auth 来管理我网站的用户。 但现在,我正在开发“设置页面”,用户可以在其中编辑他的名字、姓氏和电子邮件地址。但在设置页面中,我还想要一个“新闻通讯”复选框。
问题是: 1)我应该将newsletter字段放在数据库的哪里? 2)如何创建一个表单来编辑这些信息?
谢谢。
-- 更新 --
现在我在 models.py 中有这个:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
favourite_color = models.CharField(max_length = 40)
在 forms.py 中有这个:
class UserSettingsForm(forms.ModelForm):
class Meta:
model = User
exclude = ('password',)
def save(self, commit=False):
user = super(UserSettingsForm,self).save(commit)
favourite_color = self.cleaned_data.get('favourite_color', '')
if favourite_color and user.favourite_color is None:
UserProfile(user=user).save()
if not slug:
UserProfile.objects.filter(user=user).delete()
if not commit:
user.save()
return user
我有点困惑。我会在设置表单中编辑名字、姓氏、电子邮件和最喜欢的颜色等信息,但实际上我做错了。
Django's noob question:
I use dango.contrib.auth for managing users of my site.
But now, I'm developing the 'settings page', where an user can edit his first name, last name, and email address. But in the settings page I want also a checkbox for "newsletter".
Questions are:
1) Where should I put newsletter field in the database?
2) How can I create a form for editing these informations?
Thanks.
-- UPDATE --
Now I've this in models.py:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
favourite_color = models.CharField(max_length = 40)
and this in forms.py:
class UserSettingsForm(forms.ModelForm):
class Meta:
model = User
exclude = ('password',)
def save(self, commit=False):
user = super(UserSettingsForm,self).save(commit)
favourite_color = self.cleaned_data.get('favourite_color', '')
if favourite_color and user.favourite_color is None:
UserProfile(user=user).save()
if not slug:
UserProfile.objects.filter(user=user).delete()
if not commit:
user.save()
return user
I'm a bit confused. I would edit informations like first name, last name, email and favourite color in the settings form but actually I'm doing it wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要查看用户个人资料。
编辑:关于表格,有什么阻止你使用两种表格吗?我认为 Django 会忽略 HTTP 请求中与表单不匹配的字段,因此您可以将请求提供给两个表单。渲染模板时,Django 不会生成
在模板中:
这实际上只是一个起点,但我没有看到它无法工作的任何原因。
You want to look at user profiles.
EDIT: Regarding the forms, is there anything stopping you from using two forms? I think Django ignores fields in the HTTP request that don't match up to a form, so you could feed the request to two forms. When rendering a template, Django doesn't generate the
<form>
tags or the submit button, so you just put both forms in the same<form>
. Something like this in your view (modifying a Django example):And in the template:
This is really just a starting point, but I don't see any reason it can't work.
您还可以使用继承并让 django 管理保存。
这将在 UserProfile 上创建一个指向用户的一对一字段。 User 字段将保存在 User 模型中,其他字段将保存在 UserProfile 模型中。
You can also use inheritance and let django manage the saving.
This will create a one-to-one field on UserProfile pointing to User. The User fields will be saved in the User model and the other field in the UserProfile model.
创建一个引用 User 模型的新模型,并基于 User 创建一个 ModelForm,但包含其他字段。
您还需要将有关订阅信息的初始数据传递给表单创建:
这应该可以解决它。目前我身边没有个人例子,但这是凭记忆完成的。如果我错过了任何内容,我会在今天晚些时候尝试更新。
Create a new model with a reference to the User model, and create a ModelForm based on the User, but includes additional fields.
You'll also want to pass in initial data regarding subscription information to the form creation:
That should take care of it. I don't have a personal example right next to me at the moment, but that's done from memory. I'll try and update it later today if I've missed anything.
已解决:
在 forms.py 中:
和在views.py 中:
SOLVED:
this in forms.py:
and in the views.py:
我不知道这是否是一个好方法。但是您不能为您的用户配置文件创建特殊的类(或者您在数据库中还有一张表,因此用户搜索速度较慢)。
我尝试做下一个:
在这种情况下,我们的数据库中只有一个表(问题是:添加的字段在 django admin 中不可见)。
我会寻找问题的决定。
I don't know if this is a good way. But you can not to create special class for you User profile (or you have one more table in database so user search is slower).
I tried to do the next:
In this case we have only one table in a database (problem is: added fields is not visible in django admin).
I will search a decision of the problem.