使用信号向 django-registration 添加额外字段
我想将区域设置添加到默认的 django-registration。我尝试按照 dmitko 的教程进行操作。表单显示正确,但未保存附加数据(区域设置)。
我定义了一个自定义模型:
class AnymalsProfile(models.Model):
user = models.ForeignKey(User, unique=True)
locale = models.CharField(max_length=2)
def __unicode__(self):
return u'%s %s' % (self.user, self.locale)
和表单:
from models import AnymalsProfile
from registration.forms import RegistrationFormTermsOfService
class UserRegistrationForm(RegistrationFormTermsOfService):
locale = forms.CharField(max_length=3, widget=forms.Select(choices=LOCALE_CHOICES),label='Language:')
字段显示正确,但区域设置数据(配置文件)未保存。我认为 regbackend.py 是我的问题:
from anysite.models import AnymalsProfile
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
data = AnymalsProfile(user=user)
data.locale = form.cleaned_data["locale"]
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
* 编辑* 我尝试进入生产环境——只是为了测试——但它引发了一些错误。我更改了代码,但配置文件仍然没有保存。这是我尝试过的:
from anysite.models import AnymalsProfile
from anysite.forms import UserRegistrationForm
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
if form.is_valid():
ProfileData = form.cleaned_data
profile = AnymalsProfile(
user = user.id,
locale = ProfileData["locale"]
)
profile.save()
from registration.signals import user_registered
user_registered.connect(user_created)
I want to add a locale selection to the default django-registration. I tried to follow this tutorial from dmitko. The form shows up correctly but the additional data (locale) is not saved.
I defined a custom model:
class AnymalsProfile(models.Model):
user = models.ForeignKey(User, unique=True)
locale = models.CharField(max_length=2)
def __unicode__(self):
return u'%s %s' % (self.user, self.locale)
and the form:
from models import AnymalsProfile
from registration.forms import RegistrationFormTermsOfService
class UserRegistrationForm(RegistrationFormTermsOfService):
locale = forms.CharField(max_length=3, widget=forms.Select(choices=LOCALE_CHOICES),label='Language:')
The fields show up correctly, but the locale data (profile) is not saved. I assume that the regbackend.py is my problem:
from anysite.models import AnymalsProfile
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
data = AnymalsProfile(user=user)
data.locale = form.cleaned_data["locale"]
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
* EDIT *
I tried moving into production - just for a test - and it raised some errors. I altered the code, but still the profile is not saved. Here is what I tried:
from anysite.models import AnymalsProfile
from anysite.forms import UserRegistrationForm
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
if form.is_valid():
ProfileData = form.cleaned_data
profile = AnymalsProfile(
user = user.id,
locale = ProfileData["locale"]
)
profile.save()
from registration.signals import user_registered
user_registered.connect(user_created)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中是否有
import regbackend
.应该这样做才能执行以下字符串。在我的示例中,我在
urls.py
中有import regbackend
。你也有这条线吗?Do you have somewhere in your code
import regbackend
. That should be done in order to the following strings being executed.I my example I have
import regbackend
inurls.py
. Do you have this line as well?我不知道为什么,但它不喜欢cleaned_data。现在它可以使用以下内容运行:
感谢@dmitko 提供的代码和支持。继续努力吧!
I don't know why, but it didn't like cleaned_data. It now works using the following:
Thanks @dmitko for the code and the support. keep it up!