在 Django 中更改用户电子邮件而不使用 django-profiles

发布于 2024-08-29 17:43:34 字数 221 浏览 6 评论 0原文

在我的 Django 应用程序中,我希望用户能够更改电子邮件地址。我已经看到 StackOverflow 的解决方案将人们指向 django-profiles。不幸的是,我不想使用成熟的配置文件模块来完成更改用户电子邮件的微小壮举。有谁在任何地方看到过这个实施吗?在这种情况下,通过发送确认电子邮件来验证电子邮件地址的过程是必需的。

我花了很多时间试图找到一个有效的解决方案,但没有成功。

干杯。

In my Django application I would like the user to be able to change the email address. I've seen solution of StackOverflow pointing people to django-profiles. Unfortunately I don't want to use a full fledged profile module to accomplish a tiny feat of changing the users email. Has anyone seen this implemented anywhere. The email address verification procedure by sending a confirmation email is a requisite in this scenario.

I've spent a great a deal of time trying to a find a solution that works but to no avail.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鱼窥荷 2024-09-05 17:43:34

电子邮件地址属于用户模型,因此无需使用配置文件来让用户更新其电子邮件。从用户模型创建一个 模型表单 email 作为字段,然后在您的视图中提供该表单并进行处理。类似于:

class UserInfoForm(forms.ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('email',)

def my_view(request):
    if request.POST:
        user_form = UserInfoForm(request.POST, instance=request.user)
        if user_form.is_valid():
            user_form.save()
            return HttpResponseRedirect(reverse('form_success_screen'))
    else:
        user_form = UserInfoForm()
    return render_to_response('my-template.html', {'user_form': user_form}, context_instance=RequestContext(request))

请注意,如果表单成功,我将返回 HttpResponseRedirect 以将用户发送到另一个页面。在本例中,我从 urls 文件中查找命名的 url。

The email address belongs to the user model, so there's no need to use profiles to let users update their email. Create a model form from the user model that has only email as a field, then provide that form in your view and process it. Something like:

class UserInfoForm(forms.ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('email',)

def my_view(request):
    if request.POST:
        user_form = UserInfoForm(request.POST, instance=request.user)
        if user_form.is_valid():
            user_form.save()
            return HttpResponseRedirect(reverse('form_success_screen'))
    else:
        user_form = UserInfoForm()
    return render_to_response('my-template.html', {'user_form': user_form}, context_instance=RequestContext(request))

Note that if the form is successful, I'm returning a HttpResponseRedirect to send the user to another page. In this case, I'm looking up a named url from my urls file.

叹倦 2024-09-05 17:43:34

看来是有人自己写的。它允许用户更改他们的电子邮件地址并发送确认电子邮件。

django-emailchange

Seems that someone did write their own. It allows users to change their email addresses and sends confirmation emails.

django-emailchange

少女七分熟 2024-09-05 17:43:34

不太确定这是否是 Django 1.4 的新要求,但我尝试了 Tom 的建议:

class UserInfoForm(forms.ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('email')

并收到字段错误。每个字段后面必须有一个尾随逗号(或者这就是解决我的错误的原因):

fields = ('email',) 

解决了问题

Not quite sure if it's a new requirement of Django 1.4 but I tried Tom's suggestion:

class UserInfoForm(forms.ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('email')

and got a fields error. You must have a trailing comma after each field (or so that's what solved my error):

fields = ('email',) 

fixed the problem

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