Today i met very difficult problem in my project. Idea is simple, in my Doctor model i have a unique_connect_token, which needs for link Patient to the specific Doctor. In my patient profile template i created a simple POST method form to obtain this token from Patient. Patient see this form in his profile if he have no linked Doctor and authenticate. I have problem with my post method in this class:
class PatientProfile(generic.DetailView):
model = Patient
template_name = 'patient_profile.html'
context_object_name = 'patient'
@staticmethod
def users_role(request):
user = request.user
is_patient = user.groups.filter(name='Patients').exists()
is_doctor = user.groups.filter(name='Doctors').exists()
return is_patient, is_doctor
def get_context_data(self, **kwargs):
self.object = self.get_object()
context = super().get_context_data(**kwargs)
is_patient, is_doctor = self.users_role(self.request)
context['is_patient'] = is_patient
context['is_doctor'] = is_doctor
context['link_form'] = DoctorLinkForm()
return context
def get_object(self, queryset=None):
if not hasattr(self, 'object'):
self.object = super().get_object(queryset)
return self.object
def post(self, request, *args, **kwargs):
patient_id = request.user.id
link_form = DoctorLinkForm(request.POST)
if link_form.is_valid():
token = link_form.cleaned_data['doctor_unique_token']
doctor = Doctor.objects.get(unique_connect_token = token)
patient = Patient.objects.get(pk = patient_id)
patient.doctor_id = doctor
patient.save()
return redirect('patient_profile', pk=request.user.id)
return self.render_to_response(self.get_context_data(link_form = link_form))
When POST method works succesfully, my patient redirecting on the same page but UNLOG from site. And when im trying to login again, I get a message that the password is incorrect, although the data is entered 100% correctly. Help please, im really dont understand how to solve this issue.
Im already try use this request.user.refresh_from_db(), but it doesn’t help me