Reputation: 45
hey i want to create a profile page for my user,in which when people logging to the website they can view the profile of every user,i get the above error anytime i tried to log on to every profile of the user i get it, this is my code below
class DoctorDetailView(LoginRequiredMixin, DetailView):
model = Doctor
fields = ['user', 'email', 'image', 'speciality', 'bio']
template_name = 'pages/doctor_detail.html'
def get_queryset(self):
user = get_object_or_404(Doctor, username=self.kwargs.get('username'))
return Doctor.objects.filter(doctor=user.doctor)
path('doctor/', doctor, name='doctor'),
path('doctor/info/<str:username>', user_views.DoctorDetailView.as_view(), name='doctor-detail'),
<a href="{% url 'doctor-detail' doc.user.username %}"><div class="img-wrap d-flex align-items-stretch">
<div class="img align-self-stretch" style="background-image: url({{ doc.user.doctor.image.url }}"></div>
class CustomUser(AbstractUser):
is_doctor = models.BooleanField(default=False)
def __str__(self):
return self.email
class Status(models.Model):
title= models.CharField(max_length=5)
def __str__(self):
return self.title
class Doctor(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, related_name="doctor")
image = models.ImageField(default='jazeera.jpg', upload_to='profile_pics')
bio = models.TextField()
speciality = models.CharField(max_length=300)
describtion = models.CharField(max_length=100)
status = models.ManyToManyField(Status)
Upvotes: 1
Views: 205
Reputation: 476584
A Doctor
object has no username, hence:
user = get_object_or_404(Doctor, username=self.kwargs.get('username'))
makes not much sense, you however do not need to use get_object_or_404
to fetch the user first, you can filter with:
from django.shortcuts import get_object_or_404
class DoctorDetailView(LoginRequiredMixin, DetailView):
model = Doctor
fields = ['user', 'email', 'image', 'speciality', 'bio']
template_name = 'pages/doctor_detail.html'
def get_object(self):
return get_object_or_404(Doctor, user__username=self.kwargs['username'])
Upvotes: 1