Purpose: For the first time login user /incomplete profile user, directly website will redirect to profile completion page, then after submitting they will they go their profile page and if any changes required they will edit the required information by clicking the edit button and after completion only they will access the portal.
Problem::after submitting the complete profile form, it is redirecting to login page again, if even login again also it is showing same profile completion page. after submission, it is redirecting to Login not directly to profile
following are the codes i have created for this purpose, i am not seeing any errors in the code with my knowledge, if any one help me out this, its a big help.
codeeditor:Visual Studio 2022
django framework: latest
template using: Django web project in visual studio 2022
I have created a middleware.py with this code to validate the user
from django.shortcuts import redirect
from django.urls import reverse
from .models import UserProfile
class ProfileCompletionMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if (
request.user.is_authenticated and
not request.user.is_superuser and
not request.session.get('profile_completed', False) # Check if profile_completed session variable is False
):
try:
profile = request.user.userprofile
if not profile.is_complete():
if request.path != reverse('profile_completion'):
return redirect('profile_completion')
except UserProfile.DoesNotExist:
if request.path != reverse('profile_completion'):
return redirect('profile_completion')
response = self.get_response(request)
if (
request.user.is_authenticated and
not request.user.is_superuser and
request.path == reverse('profile_completion') and
response.status_code == 302 # Check if the response is a redirect
):
request.session['profile_completed'] = True # Set the profile_completed session variable to True
return response
then registered in settings.py file with ‘app_name.middleware.ProfileCompletionMiddleware’ in MIDDLEWARE.
then created a view in views.py file as
class ProfileView(DetailView, LoginRequiredMixin):
model = User
template_name = 'organic/profile.html'
def get_object(self, queryset=None):
return self.request.user
class ProfileCompletionView(UpdateView, LoginRequiredMixin):
model = User
template_name = 'organic/profile_completion.html'
form_class = UserRegistrationForm
success_url = reverse_lazy('profile')
def get_object(self):
return self.request.user
def form_valid(self, form):
response = super().form_valid(form)
return redirect(reverse('profile'))
then created urls:
path('profile/', login_required(views.ProfileView.as_view()), name='profile'),
path('profile_completion/', login_required(views.ProfileCompletionView.as_view()), name='profile_completion'),
profile_completion.html
{% extends 'app_name/layout.html' %}
{% block content %}
<h2>Complete Your Profile</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
profile.html
{% extends 'app_name/layout.html' %}
{% block content %}
<h2>Profile Details</h2>
{{profile.as_table}}
{% endblock %}
forms.py
class UserRegistrationForm(UserCreationForm):
name=forms.CharField(max_length=100)
address=forms.CharField(max_length=255)
country=forms.CharField(max_length=100)
class Meta:
model=User
fields=['password1','password2','name','address','country']
model.py
class UserProfile(models.Model):
USERCHOICES = [
('test', 'test'),
]
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_type = models.CharField(max_length=100, choices=USERCHOICES)
Name = models.CharField(max_length=100, unique=True)
Address = models.CharField(max_length=255)
Country = models.CharField(max_length=100)
def __str__(self):
return self.user.username
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
def is_complete(self):
# Add your logic here to determine if the profile is complete
# Return True if the profile is complete, False otherwise
return (
self.Name and
self.Address and
self.country
)
models.signals.post_save.connect(create_user_profile, sender=User)
i have all the required imports.