我正在处理Django应用程序。该应用程序拥有一个表单,填写后将重定向到 item_list 页面,用户可以在其中查看他们发布的项目,也可以删除该项目。我希望此页面仅列出当前登录的特定用户发布的项目。但是,现在,此页面列出每个用户的项目。我尝试在模板中添加if大小写,但这导致不显示任何帖子。我究竟做错了什么?到目前为止,这是我的代码
items_list模板
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %} items {% endblock title %}
{% block header %}
<link rel="stylesheet" href="{% static 'css/item_list.css' %}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'js/item_list.js' %}"></script>
{% endblock header %}
{% block content %}
<div class="container">
<center><h2>Items</h2></center>
<table class='table table-borderless table-hover'>
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Pattern</th>
<th>Color</th>
<th>Vendor</th>
<th>Upload date</th>
<th>Time</th>
<th></th>
<th></th>
</tr>
</thead>
{% for item in items %}
{% if request.user == item.vendor %}
<tr>
<td>
<a href="{{item.img.url}}" target="_blank"><img src="{{item.img.url}}" alt="{{item.title}}" style="width:80px;"></a>
</td>
<td>{{item.title}}</td>
<td>{{item.pattern}}</td>
<td>{{item.color}}</td>
<td>{{item.vendor}}</td>
<td>{{item.date}}</td>
<td>{{item.time}}</td>
{% endif %}
<td>
<a href="{% url 'upload_item' %}" class='btn btn-outline-warning btn-sm text-dark' style="width:100px">Edit item</a>
</td>
<td>
<button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal" style="width:100px">Delete item</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="card-header"><center><h5 class="modal-title">Are you sure you want to delete this item?</h5></center></div>
<div class="modal-body" style="height:200px">
<center>
<br>
<form method="post" action="{% url 'delete_item' item.pk %}" style="margin-top:10%;">
{% csrf_token %}
<button type='submit' class="btn btn-danger" style="width:200px">Delete item</button>
<button type="button" class="btn btn-outline-primary" data-dismiss="modal" style="width:200px">Cancel</button>
</form>
</center>
</div>
<div class="card-footer text-muted">
<center>Once an item is deleted, It cannot be retrieved</center>
</div>
</div>
</div>
</div>
</td>
我正在尝试根据request.user
和item.vendor
过滤项目。但这不显示任何项目。
views.py
def upload_item(request):
if request.method == 'POST':
form_des = ItemForm(request.POST, request.FILES)
if form_des.is_valid():
form_des.save()
return redirect('item_list')
else:
form_des = ItemForm()
form_des.fields['vendor'].widget.attrs['value'] = request.user
form_des.fields['vendor'].widget.attrs['readonly'] = True
return render(request, 'upload_item.html', {'form_des': form_des})
def item_list(request):
items = Item.objects.all()
return render(request, 'item_list.html', {'items':items})
在views.py中的upload_item函数下,我已将vendor字段设为只读,并自动填充到发布该项目的用户,以使其无法更改。
我在做什么错?请帮助我
谢谢
答案 0 :(得分:2)
您应该在视图中进行过滤:
def item_list(request):
items = Item.objects.filter(vendor=request.user)
return render(request, 'item_list.html', {'items':items})