Reputation: 964
I am working on a django application. The application holds a form which when filled will redirect to item_list page where the user can view the item they posted and also delete the item. I want this page to list only the items posted by that particular user who is currently logged in. but right now, this page lists items by every user. I tried adding an if case to the template but this results in displaying none of the posts. what am I doing wrong? this is my code so far
items_list template
{% 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>
I am trying to filter the items based on request.user
and item.vendor
. But this displays none of the items.
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})
under upload_item function in views.py, I have made the vendor field readonly and autofilled to the user posting the item so that it cannot be changed.
What am I doing wrong? Please help me
Thank you
Upvotes: 0
Views: 328
Reputation: 50786
You should do the filtering in your view:
def item_list(request):
items = Item.objects.filter(vendor=request.user)
return render(request, 'item_list.html', {'items':items})
Upvotes: 2