Issue
All bojects have been successfully save in admin dashboard but do not receive dynamic data in index.html page from admin dasboard.
**views.py**
from django.shortcuts import render
from .models import home
# Create your views here.
def home(request):
homeobject = home.objects.all()
return render(request, 'index.html', {'homes': homeobject})
**models.py**
class home(models.Model):
HOME_CATEGORY = [
('SR', 'Service'),
('VL', 'Vlog'),
('CR', 'course'),
]
title = models.CharField(max_length=120)
description = models.TextField()
image = models.ImageField(upload_to='images')
home_category = models.CharField(max_length=2, choices=HOME_CATEGORY)
**index.html**
{% for home in homes %}
{{home}}
{% endfor %}
Solution
your view function name is defined as home
and your model is named home
you define home
as a function so when you go to access home.objects
python does not see your model home
but the function home
Change the class name of model from home
to Home
and do migrations - migrate . Also don't forget to change in query Home.objects.all()
index.html
{% for home in homes %}
<h1> {{home.title}} </h1>
<p> {{home.description}} </p>
<img src={{home.image.url}} />
{% endfor %}
Answered By - sunil ghimire
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.