简体   繁体   中英

trouble passing django variables to my template

my views.py file

    def NoticeView(request):
        notices=Notice.objects.all()
        context={'notices':notices}

        return render(request,'aider/notice.html',context)

my model

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

    def __str__(self):
        return self.headline

    def get_absolute_url(self):
        return reverse('Notice-detail',args=[str(self.id)])

my template

<div>
    <ul>
    {% for notices in notices %}
        <li>
            <div">
                <span><h5>{{ Notice.headline}}</h5></span>
                <p>{{Notice.notice_text}}</p>
                <p><span> Posted On {{Notice.publication_date}}</span></p>
            </div>
        </li>
    {% endfor %}
    </ul>
</div>

The template shows a proper for loop iteration but is blank on where variables should be, i hope it my question makes sense...thanks

views.py is correct. Look at your for loop in template file. Do not use for notices in notices as it wrong. Use different variable names.

<div>
    <ul>
    {% for notice in notices %}
        <li>
            <div>
                <span><h5>{{ notice.headline }}</h5></span>
                <p>{{ notice.notice_text }}</p>
                <p><span> Posted On {{ notice.publication_date }}</span></p>
            </div>
        </li>
    {% endfor %}
    </ul>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM