#solved Data not showing on Django Template
(Source/Credits: https://dev.to/highcenburg/data-not-showing-on-django-template-39jj)
I don't know why it isn't showing on the templates when I think I implemented it right. the template...
I don't know why it isn't showing on the templates when I think I implemented it right.
the template is : ```
<div class="team-title">
<h3><a href="#">{{ volunteer_name.name }}</a></h3>
</div><!-- /.team-title -->
</div><!-- /.team-wrapper -->
{% endfor %}
</div><!-- /.col-md-4 -->
</div><!-- /.row -->
models.py is:
class Volunteer(models.Model):
image = models.ImageField(max_length=100, upload_to='volunteer/')
name = models.CharField(max_length=100)
facebook = models.CharField(max_length=100, validators=[URLValidator])
twitter = models.CharField(max_length=100, validators=[URLValidator])
linkedin = models.CharField(max_length=100, validators=[URLValidator])
def __str__(self):
return self.name
views.py
def home(request):
amlvideo = AMLVideo.objects.filter().order_by("-category", "-language", "-level")
volunteer_name = Volunteer.objects.all()
img = Volunteer.objects.all()
context = {"home": home}
return render(request, "aml/home.html", context)
```
Can someone help me?
Comments section
hamatti
•May 1, 2024
Your view is not sending any sensible data. It's sending the view function itself.
Try this:
``` def home(request): volunteer = Volunteer.objects.all() return render(request, 'aml/home.html', { 'volunteer': volunteer })
```
I think that should display the data correctly. At that point, we can refactor a bit to make the code easier to read. Now you're using
volunteer
(in your template) as a variable name but in view, it contains all volunteers.So maybe renaming the
volunteer
intovolunteers
and then in your template code:``` {% for volunteer_name in volunteer %}
```
into
``` {% for volunteer in volunteers %}
```
and then replacing all
volunteer_name
withvolunteer
since you're passing in the entire model object, not just their name.Let me know if the first bit works!
hamatti
•May 1, 2024
After the refactoring, it would look like this
Template:
```
{{ volunteer.name }}
```
View:
``` def home(request): volunteers = Volunteer.objects.all() return render(request, "aml/home.html", { 'volunteers': volunteers })
```
(the model looks good as is)
thedevtimeline
•May 1, 2024
I think it is small loop issue
```
```
and context should be
``` context = {"volunteer_name": volunteer_name}
```
highcenburg Author
•May 1, 2024
Fixed it already with this plus the
<img src="{{ volunteer.image.url }}" class="img-responsive" alt="Image">
Now the image doesn't show
olaoluwa98
•May 1, 2024
Your
view
is not well structured. I see you repeating Volunteer.objects.all() twice. Also, your context is sending in arequest
obj, you are supposed to send the data that you fetched from your database i.eVolunteer.objects.all()
. Also what's theamlvideo = AMLVideo.objects.filter().order_by("-category", "-language", "-level")
for? You are not sending it to the templatehighcenburg Author
•May 1, 2024
Ok so I removed the other Volunteer.objects.all() already. I would send those to the template. Just not on this model. How am I supposed to send the data form my database then?
taleb
•May 1, 2024
Can you try this.
``` render(request, "aml/home.html", {'volunteer_name': volunteer_name, })
```
Enter fullscreen mode
Exit fullscreen mode
highcenburg Author
•May 1, 2024
Data still didn't show. Do you think it's a bug on Django 2.2.4?
taleb
•May 1, 2024
you are setting context to home but home is set to anything. maybe you want
context = {'volunteer_name': volunteer_name, }
highcenburg Author
•May 1, 2024
Hey thanks for replying. I tried this but is still not showing data on the front.
Vicente G. Reyes
Python Developer | Technical Writer | Tag Moderator #Python #Django #Beginners #Opensource | Valorant Tonypoppins #881488
5 Followers
5
10
4