Home > Mobile >  Please stuck for two days on this custom class detail class view is not display record
Please stuck for two days on this custom class detail class view is not display record

Time:03-09

enter image description herePlease can anyone help me out on this issue Please stuck for two days on this custom class detail class view is not display 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 >
      <div >
        <div >
          <div >
            <h4>List of Candidate Registered</h4>
          </div>
          <div >
            <div >
              <table  
                  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 %}" ><i ></i> 
       </a></td>
 <td><a href="{% url 'emisapp:profile-page' 
c.admin.id %}" ><i > 

{% endfor %}

profile-page.html
<div  id="myTab3Content">
              <div  id="about" 
                  role="tabpanel" aria-labelledby="home-tab2">
                <div >
                  <div >
                    <strong>Full Name</strong>
                    <br>
                    <p >{{ c.admin.first_name }} 
                   </p>
                  </div>
                  <div >
                    <strong>Mobile</strong>
                    <br>
                    <p >{{ c.parentno }}</p>
                  </div>
                  <div >
                    <strong>Email</strong>
                    <br>
                    <p >{{ c.admin.email }}</p>
                  </div>

CodePudding user response:

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  id="myTab3Content">
          <div  id="about" 
              role="tabpanel" aria-labelledby="home-tab2">
            <div >
              <div >
                <strong>Full Name</strong>
                <br>
                <p >{{ candidate.first_name }} 
               </p>
              </div>
              <div >
                <strong>Mobile</strong>
                <br>
                <p >{{ candidate.admin.parentno }}</p>
              </div>
              <div >
                <strong>Email</strong>
                <br>
                <p >{{ candidate.email }}</p>
              </div>
  • Related