【问题标题】:Change the status being shown when either one of the button has been selected in a table Django更改在表格 Django 中选择任一按钮时显示的状态
【发布时间】:2023-03-14 01:23:01
【问题描述】:

我可以知道为什么当为特定行选择“批准”或“拒绝”按钮时我的状态保持不变?如何将我在 views.py 中更改的值传递回我的 html 文件以打印出状态?我应该在 models.py 中声明 Action 字段,因为 action 只是两个按钮,名称分别为批准和拒绝?

.html 文件

<form method="POST" action="">
    {% csrf_token %}
    <table  id="example" class="display" cellspacing="0" width="100%" border="1.5px">
        <tr align="center">
            <th> Student ID </th>
            <th> Student Name </th>
            <th> Start Date </th>
            <th> End Date </th>
            <th> Action </th>
            <th> Status </th>
        </tr>
        {% for item in query_results %}
        <tr align="center">
            <td> {{item.studentID}} </td>
            <td> {{item.studentName}} </td>
            <td> {{item.startDate|date:'d-m-Y'}} </td>
            <td> {{item.endDate|date:'d-m-Y'}} </td>
            <td> <input type="submit" name="approve" id="approve" value="approve" > <input type="submit" name="deny" id="deny" value="deny" > </td>
            <td> 
            {% if not item.action %}
                    Pending
                 {% endif %}        
            </td>
        </tr>
        {% endfor %}
    </table>
</form> 

当我尝试在控制台中打印 query_results 时,我看不到状态已更改。

views.py

def superltimesheet(request):
    query_results = Timesheet.objects.all()
    data={'query_results':query_results}
    supervisor = SuperLTimesheet.objects.all()[0]
    if request.method == 'POST':
        if request.POST.get('approve'):
            supervisor.status = "approved"
            supervisor.save()
            return redirect('/hrfinance/superltimesheet/')
        elif request.POST.get('deny'):
            supervisor.status = "denied"
            supervisor.save()
            return redirect('/hrfinance/superltimesheet/')
    return render(request, 'hrfinance/supervisor_list_timesheet.html', data)

模型.py

class SuperLTimesheet(models.Model):
    timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE, null=True)
    status = models.TextField("Status", default="pending", null=True)

【问题讨论】:

    标签: python html django button html-table


    【解决方案1】:

    使用 if 条件来捕获 post 方法,例如:

    def superltimesheet(request):
        query_results = Timesheet.objects.all()
        data={'query_results':query_results}
        supervisor = SuperLTimesheet()
        if request.method == 'POST':
            if request.POST.get('approve'):
                supervisor.status = "approved"
                supervisor.save()
                return redirect('/hrfinance/superltimesheet/')
            elif request.POST.get('deny'):
                supervisor.status = "denied"
                supervisor.save()
                return redirect('/hrfinance/superltimesheet/')
    
    
        return render(request, 'hrfinance/supervisor_list_timesheet.html', data)
    

    你的错误在 var supervisor,它必须是一个像 SuperLTimesheet.objects.get(pk=1) 这样的单个对象,你应该看到 url dispatcher

    【讨论】:

    • 这是我在我的 html 文件中调用 {{item.status}} 的方式吗?
    • TypeError at /hrfinance/superltimesheet/int() 参数必须是字符串或数字,而不是“QueryDict”,当我将views.py 更改为您所拥有的内容时,我收到此错误
    • 查看我的编辑,如果你想看到结果将 supervisor = SuperLTimesheet() 更改为 supervisor = SuperLTimesheet.objects.all()[0]
    • 是的,必须替换为您要编辑的对象的标识符
    • srry,删除文档
    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2013-07-28
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多