Home > OS >  Django DetailView: switches me to a different user
Django DetailView: switches me to a different user

Time:10-13

I have two user types, a student and a tutor.. and I have this ListView of tutors rendering their name, profile headline and bio, which works successfully, and I also put a link to those three fields redirecting to the detailed view of their profile. Now, I used cbv DetailView for rendering a detailed view of their profile, which also works fine.. but the only problem is, whenever I click on those link as a student, it switches my profile or my user type to that specific tutor, but when I click home or any pages of the website it switches back to normal. Could someone help me with this, please? Because I have search a solution for this problem since yesterday but I couldn't find a problem that similar to mine. Sorry for my english btw.

This is the list of tutors, and as you can see on the upper right, I am logged in as a student. This is the list of tutors, and as you can see on the upper right, I am logged in as a student.

Here, you can see on the upper right that it switches me to joss's profile. Here, you can see on the upper right that it switches me to joss's profile.

this is my models

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_tutor = models.BooleanField(default=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(max_length=100)
    phone_number = models.CharField(max_length=11, blank=False)
    current_address = models.CharField(max_length=100)
    image = models.ImageField(default='default_pic.jpg', upload_to=path_and_rename, verbose_name='Profile Pictures')

    def __str__(self):
        return f'{self.username}'


class StudentProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='student_profile')
    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name} Profile"


class TutorProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='tutor_profile')
    profile_headline = models.CharField(max_length=200, null=True)
    bio = models.TextField(max_length=500, blank=True, null=True)
    is_validated = models.BooleanField(default=False)
    DAYS = (('Monday', 'Monday'),
            ('Tuesday', 'Tuesday'),
            ('Wednesday', 'Wednesday'),
            ('Thursday', 'Thursday'),
            ('Friday', 'Friday'),
            ('Saturday', 'Saturday'),
            ('Sunday', 'Sunday'),)

    LANG = (('Javascript', 'Javascript'),
            ('Python', 'Python'),
            ('Swift', 'Swift'),
            ('Java', 'Java'),
            ('Sql', 'Sql'),
            ('PHP', 'PHP'),
            ('C#', 'C#'),)

    programming_languages = MultiSelectField(choices=LANG, null=True)
    availability = MultiSelectField(choices=DAYS, null=True)

    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name} Profile"

this is my views

class TutorListView(LoginRequiredMixin,ListView):
    model = User
    template_name = 'accounts/tutor_list.html'
    context_object_name = 'tutors'
    ordering = ['userrating']
    paginate_by = 5


class TutorDetailView(LoginRequiredMixin,DetailView):
    model = User
    template_name = 'accounts/tutor_detail_view.html'


class ProfileView(LoginRequiredMixin, TemplateView):
    template_name = 'accounts/profile.html'

If you guys need more information please let me know..

CodePudding user response:

Put a context_object_name = 'tutor' in your DetailView class!

  • Related