Reputation:
I've been working todo application. I get itemsfrom db and do for loop with button which can delete each item in the main.html. I just found that text does not shows but only button shows. Now I have 5 items and there are 5 buttons so I guess it does work, but I wander why item's title and writer does not shows.
models.py
from datetime import datetime
from django.db import models
class Article(models.Model):
no = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.CharField(max_length=300)
writer = models.CharField(max_length=50)
is_visible = models.BooleanField()
created_date = models.DateTimeField(editable=False, default=datetime.now())
views.py
def home(request):
items = Article.objects.filter(is_visible=True)
return render(request, 'blog/home.html', {'items': items})
home.html
{% for title in items %}
<div class="item-block">
<p class="item">{{ item.title }} | {{ item.writer }}</p>
<form action="{% url 'blog:delete' %}" method="post">
{% csrf_token %}
<p><input type="hidden" name="id" value="{{ item.id }}">
<button class="submit-button btn btn-default btn-xs" type="submit">삭제</button></p>
</form>
</div>
{% endfor %}
Upvotes: 0
Views: 61
Reputation: 73498
Your loop variable name is 'title'
(which is oddly chosen):
{% for title in items %}
But you seem to access it as 'item'
: {{ item.title }}
, {{ item.writer }}
, etc.! Change the loop variable name to item
:
{% for item in items %}
Upvotes: 1