Iam new in programming. I need to make a model/table in django where details of a User has to save. If the User login It will goes to registration page if he is not completed the registration, else if he already completed the registration it will goes to home page.
What should I do?
models.py
class UserReg(models.Model):
Name=models.CharField(max_length=200)
Date_of_Birth=models.DateField()
Age=models.IntegerField()
Gender=models.CharField(max_length=200, choices=GenderChoice)
Phone_no=models.IntegerField()
Mail=models.EmailField(unique=True)
Address=models.TextField(max_length=700)
District=models.ForeignKey(District,on_delete=models.CASCADE)
Branch=models.ForeignKey(Branch,on_delete=models.CASCADE)
Account_Type=models.CharField(max_length=200,choices=AccType)
Materials=models.ManyToManyField(Materials)
views.py
def reg(request):
form = Userform()
if request.method == 'POST':
form=Userform(request.POST)
if form.is_valid():
Name=request.POST.get('Name')
Date_of_Birth = request.POST.get('Date_of_Birth')
Age = request.POST.get('Age')
Gender = request.POST.get('Gender')
Phone_no = request.POST.get('Phone_no')
Mail = request.POST.get('Mail')
Address = request.POST.get('Address')
District = request.POST.get('District')
Branch = request.POST.get('Branch')
Account_Type = request.POST.get('Account_Type')
Materials = request.POST.get('Materials')
obj=UserReg(Name=Name,Date_of_Birth=Date_of_Birth,Age=Age,Gender=Gender,Phone_no=Phone_no,Mail=Mail,Address=Address,District=District,Branch=Branch,Account_Type=Account_Type,Materials=Materials)
obj.save()
return redirect('/')
return render(request,'registration.html',{'form':form,})
CodePudding user response:
First you need to bind UserReg to User model by adding OneToOne relation field to your UserReg model:
class UserReg(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# rest of your fields
You can create modelForm for it:
class UserRegForm(forms.ModelForm):
class Meta:
model = UserReg
fields = '__all__'
widgets = {'user': forms.HiddenInput }
In reg view first check if user have UserReg object and if not go with the view:
def reg(request):
if request.user.userreg:
return redirect ('/')
if request.method == 'POST':
form = UserRegForm(request.POST)
if form.is_valid():
form.save()
return redirect ('/')
else:
form = UserRegForm(initial={'user': request.user})
return render(request,'registration.html',{'form':form,})
CodePudding user response:
I think best approach is to make a costume decorator
#in your UserReg add a new field User
class UserReg(models.Model):
user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
...otherfields
don't forget to import User and instantiate The user Above ur UserReg Model
from django.contrib.auth import get_user_model
User = get_user_model()
now make your migrations run these in the command line care when you do this command you will have an error if there is already rows in your database in table UserReg to avoid please delete all the database and run the commands again if they ran without error you are good to go
python manage.py makemigrations
python manage.py migrate #
now create a new file in the same directory call it decorator.py and put this code inside which we will use to prevent user from viewing any page u want without filling his data
def complete_registration(view_func):
def wrapper_func(request, *args, **kwargs):
if request.user.userreg.exists(): # here we check if has a userreg profile if he has we could extra check if he had filled all fields if he passed return him to view
return view_func(request, *args, **kwargs)
else:
return redirect('registration') # else he didnt pass if checks redirect him to profil registration page
return wrapper_func
now we made our decorator lets use it on all views we want to restrict user to visit without filling his profile
# lets import our newly decorator
from .decorators import complete_registration
lets assign the decorator to every view i want to restrict user from visiting without filling profile
@complete_registration
def viewname(request):
return
@complete_registration
def anotherview(request):
return