我創建了一個頁面,當員工點擊查看按鈕時,它應該將他們重定向到查看頁面,但我得到這個錯誤'照片'物件不可迭代,如下圖所示。我怎樣才能解決這個錯誤呢?
views.py
def view(request, pk):
alldescription = Photo.objects.get(id=pk)
return render(request, 'view.html', {'alldescription': alldescription})
urls.py
urlpatterns = [
path('register/', views.register, name='register')。
path('adminpage/', views.admin, name='adminpage')。
path('customer/', views.customer, name='customer')。
path('logistic/', views.logistic, name='logistic')。
path('forget/', views.forget, name='forget')。
path('changepassword/', views.changepassword, name='changepassword')。
path('newblock/', views.newblock, name='newblock')。
path('quote/', views.quote, name='quote')。
path('profile/', views.profile, name='profile')。
path('adminprofile/', views.adminprofile, name='adminprofile')。
path('', views.login_user, name='login')。
path('home/', views.home, name='home')。
path('allstaff/', views.allstaff, name='allstaff')。
path('updatetaff', views.updatetaff, name='updatetaff')。
path('delete/<int:id>/', views.delete, name='delete')。
path('deletephoto/<int:id>/', views.deletephoto, name='deletephoto')。
path('update/<int:id>/', views.update, name='update')。
path('logout/', views.logout_view, name='logout')。
path('register/', views.register_view, name='register')。
path('edit-register/', views.edit_register_view, name='edit_register')。
path('edit_profile/', views.edit_profile, name='edit_profile')。
path('ReceptionUnserviceable/', views.ReceptionUnserviceable, name='ReceptionUnserviceable')。
path('success', views.success, name='success')。
path('logisticprofile', views.logisticprofile, name='logisticprofile')。
path('viewreception/', views.viewreception, name='viewreception')。
path('view/<str:pk>/', views.view, name='view')。
path('outgoingLRU/', views.outgoingLRU, name='outgoingLRU')。
]
urlpatterns = static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
viewreception.html
{% extends "logisticbase.html" %}.
{% block content %}
<style>
table {
border-collapse:分離。
border: solid black 1px;
border-radius:6px;
-moz-border-radius: 6px;
}
td, th {
border-left: solid black 1px;
border-top: solid black 1px;
}
th {
border-top: none;
}
td:first-child, th:first-child{
border-left: none;
}
h4{color: #006E33; }
</style>>
<div style="padding-left:16px"/span>>
<br>
<h4>接待處無法使用 </h4>/span>
<p>要洗掉,請點擊洗掉按鈕。</p>/span>
<div class="form-block"/span>>
</tr>/span>
{% for Photo in alldescription %}.
<div class="col-md-9">
<div class="row">
<div class="col-md-5"/span>>
<div class="card my-2"/span>>
<img class="image-thumbail" src="/media/{{Photo. image}}" width="250px">
<br>/span>
<div class="card-body">
<small>客戶名稱。{{Photo.customername}}</small>客戶名稱:small>
<br>/span>
<small>日期和時間: {{Photo.datetime}}</small>
</div>/span>
<form action="{% url 'view' Photo. id %}" method="post">
{% csrf_token %}
< button type="submit" class="btn btn-sm btn-info" style="width: 460px">查看</按鈕>
</form>/span>
<br>/span>
<form action="{%url 'deletephoto' Photo. id %}" method="post">
{% csrf_token %}
< button type="submit" class="btn btn-sm btn-danger" style="width: 460px">洗掉</按鈕>/span>
</form>/span>
</div>/span>
</div>/span>
</div>/span>
</div>/span>
{% endfor %}
view.html
{% extends "logisticbase.html" %}
{% block content %}
<style>
</style>
<div style="padding-left:16px">/span>
<br>
<h4>接待處無法使用 </h4>/span>
<div class="form-block">
</tr>/span>
{% for Photo in alldescription %}.
<img src="/media/{{Photo. image}}" width="250px">
{% endfor %}
</div>
</div>/span>
{% endblock %}
uj5u.com熱心網友回復:
你使用
Photo.objects.get(id=pk)
但是get()給你回傳一個單項。在你的模板中,你試圖遍歷這一個專案。這是不可能的。所以這就是你在渲染相關模板時出錯的原因。
你應該在你的模板中使用洗掉for-loop:
<img src="/media/{{ alldescription. Photo.image }}" width="250px">
uj5u.com熱心網友回復:
你沒有回傳一個queryset,而是回傳視圖中的單個實體(由get方法表示),因此它是不可迭代的。
def view(request, pk):
alldescription = Photo.objects.get(id=pk)
return render(request, 'view.html', {'alldescription': alldescription})
這就是為什么你不能在你的模板中進行迭代。
只要使用
<img src="/media/{{ alldescription. image }}" width="250px">
并且在viewreception.html和logisticbase.html模板中也回傳其他回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/333741.html
標籤:


