[英]'post' object is not iterable in django and it is showing error but i'm not getting what's going wrong on code
/dashboard 'post' object 處的 TypeError 不可迭代我只是嘗試從 db 表中獲取數據並以 dabel 形式顯示在儀表板上
我的
dashboard.html
頁面
{% if posts %}
<table class="table table-dark table-hover">
<thead>
<tr>
<th scope="col" style="width:3%">ID</th>
<th scope="col" style="width:30%">Title</th>
<th scope="col" style="width:55%">Description</th>
<th scope="col" style="width:15%">Action</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
<tr>
<td>{{posts.id}} </td>
<td>{{posts.title}}</td>
<td>{{posts.desc}}</td>
<td><a class="btn btn-primary" type="submit" value="Submit">Edit</a>
<form action ='' , method ='POST' class='d-inline'>
{% csrf_token %}
<input type='submit' class='btn btn-danger btn-sm' value ='Delete' >
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<h4 class =' text-center alert alert-warning'>No Records </h4>
{% endif %}
</div>
</div>
{% endblock content %}
我的觀點.py
def dashboard(request):
if request.user.is_authenticated:
posts= post.objects.all()
return render(request,'dashboard.html',{'posts':post})
else:
return HttpResponseRedirect('login')
網址.py
path('dashboard',views.dashboard,name='dashboard'),
您的views.py
文件中有錯字。 嘗試改變這個:
return render(request,'dashboard.html',{'posts':post})
對此:
return render(request,'dashboard.html',{'posts':posts})
我假設在模板 dashboard.html 你必須改變這個:
{% for post in posts %}
<tr>
<td>{{posts.id}} </td>
<td>{{posts.title}}</td>
<td>{{posts.desc}}</td>
對此:
{% for post in posts %}
<tr>
<td>{{post.id}} </td>
<td>{{post.title}}</td>
<td>{{post.desc}}</td>
它應該是這樣的:
{% for post in posts %} <tr> <td>{{post.id}} </td> <td>{{post.title}}</td> <td>{{post.desc}}</td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.