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')
CodePudding user response:
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)
CodePudding user response:
Okay so thier is a python signals.py file which will handle all the stuff like that in the background you should learn about this file instead of skip this part becaus its necessary, go to the django documentaion and learn more about this.. https://docs.djangoproject.com/en/4.0/topics/signals/
@enes islam is answering your question if i will do this again so my comment will be useless so I have another little tip for you if you dont want to create signals file or import into apps.py by get ready method if you want to skip this so you just add the signals code on your models so you dont want to import on signals I think this comment will be useful for everyone who will come here after a month or year....