I am trying to register a user in django extending the auth
module. I read from here and there and now I am all over the place and dont know what I am doing. Initially what I wanted to do is register a user with information name , email , mobile and password . since mobile is not in the default django auth
I tried to extend auth
.
here is the code.
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class CustomerUserProfile(models.Model):
mobile = models.CharField(max_length = 20)
user = models.ForeignKey(User, unique=True)
def __str__(self):
return "%s's profile" % self.user
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = CustomerUserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from neededform.models import CustomerUserProfile
class CustomerRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
mobile = forms.CharField(max_length = 20)
class Meta:
model = User
fields = ('username','email','mobile','password1','password2')
views.py
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.context_processors import csrf
from neededform.forms import CustomerRegistrationForm
from neededform.models import CustomerUserProfile
from django.contrib.auth.models import User
def register(request):
if request.method == 'POST':
form = CustomerRegistrationForm(request.POST)
if form.is_valid():
f = form.save()
return HttpResponseRedirect('/registered/')
else:
args = {}
args.update(csrf(request))
args['form'] = CustomerRegistrationForm()
return render_to_response('User_Registration.html', args ,context_instance = RequestContext(request))
This code upon execution makes an entry in auth_user
table and an entry in CustomerUserProfile
but the mobile coloumn is blank always.
what else am I suppose to add in the code ?
PS. please can some one explain me what is this code doing
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = CustomerUserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
as I copied it from somewhere I want to know how this works actually.
Thank you.
This is a 2-year old question but I am surprised that there was no correct answer. The mobile field is not saved in your profile, simply because you create your CustomerUserProfile
via the post_save signal, and the mobile field is not forwarded by the post_save signal.
I would suggest to simply update your CustomerUserProfile
when you handle form.is_valid() in your register
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.