django

My custom class detail class view is not displaying a record


enter image description here

I am working on this custom class detail class view, which is not displaying a record. I think is reverting back to the logged in user data instead of the detail from the list.

My code below not error even printed out the variable but still blank

view.py

class ListOfEnrolledCandidate(View):
    def get(self, request, **kwargs):
        users = 
 CustomUser.objects.filter(user_type=6).select_related('candidates')
        context = {
        'users': users
        }
        return render(request, 'superadmin/candidates/list- 
        enrolled.html', context)

class CandidateProfile(View):
    def get(self, request, **kwargs):
        user = CustomUser.objects.get(id=int(kwargs['id']))
        print(user)
        return render(request, 'superadmin/candidates/profile- 
        detail.html',{'users':user.id})

models.py

class Candidates(models.Model):
    admin = models.OneToOneField(CustomUser, 
     on_delete=models.CASCADE, 
     related_name="candidates")
     profile_pic = models.ImageField(default='default.jpg', 
     upload_to='upload')
     middle_name = models.CharField(max_length=255)
     gender = models.CharField(max_length=255)
     country = models.ForeignKey(Country, on_delete=models.CASCADE, 
     null=True)
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    local = models.ForeignKey(Local, on_delete=models.CASCADE)
    dob = models.CharField(max_length=100)
    candclass = models.CharField(max_length=100, null=True)
    parentno = models.CharField(max_length=11, null=True)
    exam_year = models.CharField(max_length=100, null=True)
    profile_pic = models.ImageField(default='default.jpg', 
    upload_to='media/uploads')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()

    def __str__(self):
        return self.middle_name


class CandidateSubject(models.Model):    
    admin = models.OneToOneField(CustomUser, null=True, 
on_delete=models.CASCADE)
    subject1 = models.CharField(max_length=255, null=True)
    subject2 = models.CharField(max_length=255, null=True)
    subject3 = models.CharField(max_length=255, null=True)
    subject4 = models.CharField(max_length=255, null=True)
    subject5 = models.CharField(max_length=255, null=True)
    subject6 = models.CharField(max_length=255, null=True)
    subject7 = models.CharField(max_length=255, null=True)
    subject8 = models.CharField(max_length=255, null=True)
    subject9 = models.CharField(max_length=255, null=True)
    subject10 = models.CharField(max_length=255, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()

    def __str__(self):
        return f'{self.subject1, self.subject2}'

this is a one to one relationship with user

class CustomUser(AbstractUser):
    user_type_data = ((1, "HOD"), (2, "Staffs"), (3, "Teachers"), (4, 
"Parents"), (5, 
    "Students"), (6, "Candidates"))
    user_type = models.CharField(default=1, choices=user_type_data, 
max_length=15)

    def __str__(self) -> str:
        return self.first_name

i enjoy custom class more than functions please anyone help me out 
with this code

list-detail.html
<div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <h4>List of Candidate Registered</h4>
          </div>
          <div class="card-body">
            <div class="table-responsive">
              <table class="table table-striped table-hover" 
                  id="tableExport" style="width:100%;">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name </th>
                    <th>Middle Name </th>
                    <th>Date Enrolled</th>
                    <th>Class</th>
                    <th>Year</th>
                    <th colspan="4">Actions</th>
                  </tr>
                </thead>
                <tbody>
                {% for c in candidates %}
                  <tr>
                    <td>{{ c.admin.id }}</td>
                    <td>{{ c.admin.first_name }}</td>
                    <td>{{ c.admin.last_name }}</td>
                    <td>{{ c.middle_name }}</td>
                    <td>{{ c.created_at }}</td>
                    <td>{{ c.candclass }}</td>
                    <td>{{ c.exam_year }}</td>
                    <td>
  <a href="{% url 'emisapp:update-page'c.admin.id %}" class="btn btn- 
   primary"><i class="fas fa-edit"></i> 
       </a></td>
 <td><a href="{% url 'emisapp:profile-page' 
c.admin.id %}" class="btn btn-primary"><i class="fas fa-user-check"> 

{% endfor %}

profile-page.html
<div class="tab-content tab-bordered" id="myTab3Content">
              <div class="tab-pane fade show active" id="about" 
                  role="tabpanel" aria-labelledby="home-tab2">
                <div class="row">
                  <div class="col-md-3 col-6 b-r">
                    <strong>Full Name</strong>
                    <br>
                    <p class="text-muted">{{ c.admin.first_name }} 
                   </p>
                  </div>
                  <div class="col-md-3 col-6 b-r">
                    <strong>Mobile</strong>
                    <br>
                    <p class="text-muted">{{ c.parentno }}</p>
                  </div>
                  <div class="col-md-3 col-6 b-r">
                    <strong>Email</strong>
                    <br>
                    <p class="text-muted">{{ c.admin.email }}</p>
                  </div>

Solution

  • For your CandidateProfile class it may be better to use a DetailView:

    from django.views.generic.detail import DetailView
    from django.shortcuts import get_object_or_404
    
    class CandidateProfile(DetailView):
        context_object_name = 'candidate'
        template_name = 'superadmin/candidates/profile-detail.html'
        
        def get_object(self):
            return get_object_or_404(CustomUser, pk=int(self.kwargs['id']))
    

    As we have set context_object_name to candidate this means that django will pass the CustomUser object to the template, and we can access this object using candidate.

    profile-page.html will look something like this:

             <div class="tab-content tab-bordered" id="myTab3Content">
              <div class="tab-pane fade show active" id="about" 
                  role="tabpanel" aria-labelledby="home-tab2">
                <div class="row">
                  <div class="col-md-3 col-6 b-r">
                    <strong>Full Name</strong>
                    <br>
                    <p class="text-muted">{{ candidate.first_name }} 
                   </p>
                  </div>
                  <div class="col-md-3 col-6 b-r">
                    <strong>Mobile</strong>
                    <br>
                    <p class="text-muted">{{ candidate.admin.parentno }}</p>
                  </div>
                  <div class="col-md-3 col-6 b-r">
                    <strong>Email</strong>
                    <br>
                    <p class="text-muted">{{ candidate.email }}</p>
                  </div>