0

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.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

3 Answers3

2

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.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
Tom
  • 22,301
  • 5
  • 63
  • 96
  • Sorry if I seem demanding but have you come across a snippet which sends a verification email. I'm good with Python but I haven't implemented anything similar in Django. Thank you for the help though. – Mridang Agarwalla Apr 17 '10 at 12:45
  • Try django-email-confirmation: http://github.com/jtauber/django-email-confirmation/ – Tom Apr 17 '10 at 14:34
1

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

django-emailchange

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
0

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

NightCodeGT
  • 67
  • 1
  • 6