【问题标题】:Why data from table is not recieving in django?为什么表中的数据没有在 django 中接收?
【发布时间】:2021-08-28 14:26:57
【问题描述】:

当我试图从表中检索数据时,它没有显示任何内容而没有给出错误消息。

这是我的模特

class staff(models.Model):
    id = models.AutoField
    name = models.CharField(max_length=250)
    role = models.CharField(max_length=250)
    salary = models.CharField(max_length=250)
    address = models.CharField(max_length=250)
    number = models.CharField(max_length=250)
    date = models.DateField()

这是我的视图文件

def user(request):

    staffs = staff.objects.all()
    params = {'staff': staffs}
    return render(request, "salary/user.html", params)

这是我的模板

<td> 02312 </td>
{% for staff in staffs %}
    <td>{{ staff.name }} </td>
{% endfor %}
<td> $14,500 </td>

【问题讨论】:

    标签: django django-models django-views django-templates


    【解决方案1】:

    试试这个 视图.py

    def user(request):
        staffs = staff.objects.all()
        return render(request, "salary/user.html", {'staff': staffs})
    

    html

     <td> 02312 </td>
        {% for staffs in staff %}
            <td>{{ staffs.name }} </td>
        {% endfor %}
        <td> $14,500 </td>
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多