Search code examples
python-3.xdjango-modelsdjango-viewsdjango-generic-viewsdjango-3.1

Having trouble passing variables from my database to my website using Django 3


I am still a newbie to django and have recently begun on a project with numerous models. the problem is whenever i want to pass a variable from my database to my website templates when i run the site i do not see my variables, they just appear empty. for example if i have 10 object variables under a certain model, when i use a for loop to iterate through the objects within an ordered list(html) all i see on the rendered page are empty bullet points. am lost need help!!!

Example Notice model

    from django.db import models
    from django.utils import timezone
    import datetime

    class Notice(models.Model):
        headline=models.CharField(max_length=150)
        notice_text=models.TextField()
        publication_date=models.DateTimeField('date published')

        def __str__(self):
            return self.headline

The view

    class Home_pageView(generic.ListView):
        template_name = 'Notices/home_page.html'
        context_object_name = 'notice_objects'

        def get_queryset(self):
            return Notice.objects.all()

The template

    {% if notice_objects %}
    <ul>
    {% for item in notice_objects %}
    <li><a href="{% url 'notices:detail' notice.id %}">{{ notice.notice_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No notices are available.</p>
    {% endif %}

As I have already mentioned, when I run the above code all i get are empty bullet points corresponding to the number of notice objects I have in my database. I also have another model called question in my project and with that one if I use the same code the variables render properly but I don't seem to notice any difference between the two models but maybe you guys can spot any irregular differences. Otherwise all my other models besides the question model render empty bullet points on my site. I even thought my database was the issue so I changed from postgresql to mariadb and to sqlite but nothing changed.

Question model

    from django.db import models
    from django.utils import timezone
    import datetime

    class Question(models.Model):
        headline=models.CharField(max_length=150,default=None,null=True)
        question_text=models.CharField(max_length=200)
        publication_date=models.DateTimeField('date published')
        def __str__(self):
            return self.question_text
        def was_published_recently(self):
            now = timezone.now()
            return now-datetime.timedelta(days=1) <= self.publication_date <= now 

The view

    class Home_pageView(generic.ListView):
        template_name = 'Polls/home_page.html'
        context_object_name = 'latest_question_list'

        def get_queryset(self):
            return Question.objects.all()

The template

    {% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
    {% else %}
    <p>No polls are available.</p>
    {% endif %}

Thanks for your help in advance guys.


Solution

  • In your views,write a very thin line structure like.

     class Home_pageView(generic.ListView):
        template_name = 'Notices/home_page.html'
        model=Notice
    

    similar case for question list view,

    In your HTML template,

     {% for something in object_list %}
     {{something.notice_text}}