Home > Net >  Model infos not showing up on HTML page Django
Model infos not showing up on HTML page Django

Time:09-11

I am trying to create an educational website using Django, so when I am trying to render {{ profile.institution }} or {{ profile.grade }} or {{ profile.user.username }} they are not being rendered.I don't know why they aren't. Can anyone help me solve this?

My models.py:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    institution = models.CharField(max_length = 100)
    grade = models.CharField(max_length=100, choices= YEAR_IN_SCHOOL_CHOICES)
    bio = models.TextField(max_length=300)

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

My views.py:

class User_Profile(LoginRequiredMixin, ListView):
    model = Profile
    template_name = 'class/profile.html'
    context_object_name = 'profile'

    def get_queryset(self):
        return Profile.objects.filter(user=self.request.user)

My html:

{% extends "class/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <br>
    <div >
        <h1 style="color: #f5a425">Hello {{ user.username }}</h1>
    </div>

    <div >
        
        <div >
            
            <div >
                
                <div >
                    
                    <div >
                        <i class='fas fa-user-alt' style='font-size:36px'></i>
                        <!-- <img src="" width="100" > -->
                    </div>
                    
                    <div >
                        <span >Pro</span>
                        <h5 >{{ profile.user.username }}</h5>
                        <span>{{ profile.institution }}</span>
                        <span>{{ profile.grade }} Grade</span>

                        
                        <div >
                            <p >{{ profile.bio }}</p>
                        
                        </div>

                        
                        <div >
                            
                            <button >Message</button>
                            <button >Contact</button>
                        </div>
                        
                        
                    </div>
                    
                   
                    
                    
                </div>
                
            </div>
            
        </div>
        
    </div>
{% endblock content %}

CodePudding user response:

what part of the code should I change to make this work ?

ListView it is about many objects ant iteration through the object_list. In this case the answer of @PouyaEsmaeili is correct.

But. The mistake is - you have a wrong view. DetailView is the right choose. This returns always one object or nothing.

Profile.objects.filter(user=self.request.user)

In your case:

class User_Profile(LoginRequiredMixin, DetailView):
    model = Profile
    template_name = 'class/profile.html'
    context_object_name = 'profile'

    def get_object(self):
        return get_object_or_404(self.model, user=self.request.user)

If you set name for template profile_detail.html, you don't need template_name attribute. It should be find automatically. If you don't change the model_name in Profile._meta - , you don't need context_object_name attribute. It should be defined automatically. Please, don't forget about Django-views naming best practices.

Last version of your view can be:

class ProfileDetailView(LoginRequiredMixin, DetailView):
    model = Profile

    def get_object(self):
        return get_object_or_404(self.model, user=self.request.user)
  • Related