Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Model infos not showing up on HTML page Django

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 class="row d-flex justify-content-center">
        <h1 style="color: #f5a425">Hello {{ user.username }}</h1>
    </div>

    <div class="container mt-5">
        
        <div class="row d-flex justify-content-center">
            
            <div class="col-md-7">
                
                <div class="card p-3 py-4">
                    
                    <div class="text-center">
                        <i class='fas fa-user-alt' style='font-size:36px'></i>
                        <!-- <img src="" width="100" class="rounded-circle"> -->
                    </div>
                    
                    <div class="text-center mt-3">
                        <span class="bg-secondary p-1 px-4 rounded text-white">Pro</span>
                        <h5 class="mt-2 mb-0">{{ profile.user.username }}</h5>
                        <span>{{ profile.institution }}</span>
                        <span>{{ profile.grade }} Grade</span>

                        
                        <div class="px-4 mt-1">
                            <p class="fonts">{{ profile.bio }}</p>
                        
                        </div>

                        
                        <div class="buttons">
                            
                            <button class="btn btn-outline-primary px-4">Message</button>
                            <button class="btn btn-primary px-4 ms-3">Contact</button>
                        </div>
                        
                        
                    </div>
                    
                   
                    
                    
                </div>
                
            </div>
            
        </div>
        
    </div>
{% endblock content %}

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading