请在此自定义类详细信息上停留两天,类视图不是显示记录



在此处输入图像描述请有人帮我解决这个问题请在这个自定义类详细信息上停留两天,类视图不是显示记录,我think正在恢复到已登录的用户数据,而不是列表中的详细信息我下面的代码没有错误,甚至打印出了变量,但仍然是空白的

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})

型号.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}'

这是与用户的一对一关系

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>

对于CandidateProfile类,最好使用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']))

由于我们已经将context_object_name设置为candidate,这意味着django将把CustomUser对象传递给模板,我们可以使用candidate访问该对象。

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">{{ 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>

最新更新