Django authentication problem - how can I force active users to setup a profile first?
-
29-10-2019 - |
Question
I've put myself into somewhat of a pickle. I use django-registration often, and it seems to work for most situations. However, I want to require users to build their profile (eg: demographic information) before they can visit any of the other pages.
This is how I desire the current setup to run:
- visitor fills out registration form --(submit)--->
- user email verification --(link creates active user)--->
- --(redirected to profile view)--->
- user fills out profile form --(submit)-->
- user can now access the rest of the website
Is there a recommended way to do this?
Solution
One of the ways of doing it would be to use your own @profile_required
decorator rather than the django's built in login_required
on all your views.
@login_required
def profile_required(func,request,*args,**kwargs):
has_profile = request.user.profile_set.count()
if not has_profile:
return redirect('create_profile')
return func(request,*args,**kwargs)
Then on each view you want to have a user with profile visit, just:
@profile_required
def my_awesome_view(request):
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow