Python 根据django模板中的特定字段筛选帖子

Python 根据django模板中的特定字段筛选帖子,python,django,Python,Django,我正在开发django应用程序。应用程序持有一个表单,填写后将重定向到项目列表页面,用户可以在该页面查看他们发布的项目,也可以删除该项目。我希望此页面仅列出当前登录的特定用户发布的项目。但是现在,这个页面列出了每个用户的项目。我尝试在模板中添加一个if case,但结果没有显示任何帖子。我做错了什么?这是到目前为止我的代码 项目列表模板 {% extends 'base.html' %} {% load staticfiles %} {% block title %} items {% end

我正在开发django应用程序。应用程序持有一个表单,填写后将重定向到项目列表页面,用户可以在该页面查看他们发布的项目,也可以删除该项目。我希望此页面仅列出当前登录的特定用户发布的项目。但是现在,这个页面列出了每个用户的项目。我尝试在模板中添加一个if case,但结果没有显示任何帖子。我做错了什么?这是到目前为止我的代码

项目列表模板

{% 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>
在views.py中的upload_item函数下,我已将供应商字段设置为只读,并自动填充给发布该项目的用户,以便无法更改

我做错了什么?请帮帮我


谢谢

您应该在视图中进行筛选:

def item_list(request):
    items = Item.objects.filter(vendor=request.user)
    return render(request, 'item_list.html', {'items':items})

您应该在视图中进行过滤:

def item_list(request):
    items = Item.objects.filter(vendor=request.user)
    return render(request, 'item_list.html', {'items':items})

发布将数据传递到Template的视图是否使用Django ORM?您可以通过请求查询数据库筛选。userpost将数据从中传递到template的视图是否使用任何Django ORM?您可以通过请求查询数据库筛选。UserThank。这起作用了。我还有一个疑问。还有另一种类型的用户商户。该商户应该能够看到所有供应商的帖子。我该怎么做呢。现在,当以商户身份登录时,项目列表模板不显示任何帖子,必须使用
if
语句检查
请求。用户
是否为商户,并相应地设置
项目
。谢谢。这起作用了。我还有一个疑问。还有另一种类型的用户商户。该商户应该能够看到所有供应商的帖子。我该怎么做呢。现在,当以商户身份登录时,项目列表模板不显示任何帖子,必须使用
if
语句检查
请求。用户
是否为商户,并相应地设置
项目。