Reputation:
how can i create automatic user profile ? is there any way i can create automatic user profile when a user submitted the registration form In django, Like Stack Overflow. In Stack Overflow when a new user created an account with either Gmail, Facebook Or Github The Stack Overflow will create automatic user profile for He/she so He/She can edit it of His/Her choices. I know how to implement an edit functionality using UpdateView. I just want to create automatic user profile when the form is submitted. can i create an automatic user profile in django if yes then How ?
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='avatar', blank=True, null=True)
stories = RichTextField(blank=True, null=True)
twitter = models.URLField(max_length=300, blank=True, null=True)
website = models.URLField(max_length=300,blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
location = models.CharField(max_length=80, blank=True, null=True)
slug = models.SlugField(unique=True, max_length=200)
def save(self, *args, **kwargs):
self.slug = slugify(self.user)
super(Profile, self).save(*args, **kwargs)
def __str__(self):
return str(self.user)
my user registration views
def register(request):
if request.method == 'POST':
username = request.POST['username']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email or user name Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, first_name=first_name,
last_name=last_name, email=email, password=password)
user.save();
return redirect('Home')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
Upvotes: 0
Views: 479
Reputation:
for the first time I take @enes islam answer but it does not work until i add this into my apps.py file:
apps.py file:
def ready(self):
from . import signals
@enes islam answer
You need to use Django Signals
create signals.py
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from . models import Profile
User = settings.AUTH_USER_MODEL
#this function wake up after a User object save
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
Upvotes: 0
Reputation: 1119
You need to use Django Signals.
create signals.py
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from . models import Profile
User = settings.AUTH_USER_MODEL
#this function wake up after a User object save
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
Upvotes: 1