使用信号向 django-registration 添加额外字段

发布于 2024-09-19 19:04:57 字数 1788 浏览 8 评论 0原文

我想将区域设置添加到默认的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-09-26 19:04:58

您的代码中是否有 import regbackend.应该这样做才能执行以下字符串。

from registration.signals import user_registered
user_registered.connect(user_created)

在我的示例中,我在 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.

from registration.signals import user_registered
user_registered.connect(user_created)

I my example I have import regbackend in urls.py. Do you have this line as well?

冰雪之触 2024-09-26 19:04:58

我不知道为什么,但它不喜欢cleaned_data。现在它可以使用以下内容运行:

def user_created(sender, user, request, **kwargs):
        form = UserRegistrationForm(request.POST)
        data = AnymalsProfile(user=user)
        data.locale = form.data["locale"]
        data.save()

感谢@dmitko 提供的代码和支持。继续努力吧!

I don't know why, but it didn't like cleaned_data. It now works using the following:

def user_created(sender, user, request, **kwargs):
        form = UserRegistrationForm(request.POST)
        data = AnymalsProfile(user=user)
        data.locale = form.data["locale"]
        data.save()

Thanks @dmitko for the code and the support. keep it up!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文