django 使用get_queryset后,如何使发布请求正常工作?

3lxsmp7m  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(119)

我希望有一个用户设备的列表,每个设备旁边都有一个复选框。用户可以通过单击相应的复选框,然后单击提交按钮来选择他们想要在Map上查看的设备。我没有在这个问题中包括Map部分,因为我计划稍后解决这个问题。目前导致问题的步骤是尝试使用发布请求。
为了让用户只能看到分配给他们的设备,我使用了get_queryset方法。我看到了一些关于使用发布请求沿着get_queryset方法的问题,但它们似乎不适用于我。使用下面的视图,当我选中一个复选框,然后单击提交时,它看起来像是一个post请求,紧接着是一个get请求,结果是页面加载时我的表为空。
我的www.example.com文件的一部分views.py:

class DeviceListView(LoginRequiredMixin, ListView):
    model = Device
    template_name = 'tracking/home.html'
    context_object_name = 'devices'

    def get_queryset(self, *args, **kwargs):
        return super().get_queryset(*args, **kwargs).filter(who_added=self.request.user)       

    def post(self, request):
        form = SelectForm(request.POST)   
        return render(request, self.template_name, {'form': form})

我的模板部分:

<div class="table-responsive">
                    <form action="" method="post" name="devices_to_check"> 
                        {% csrf_token %}
                        <table id="registered_devices" 
                        class="table table-striped table-bordered table-hover table-sm" 
                        style="width:100%; border: 1px solid black; font-size: 10px">
                            <thead class="table-primary" 
                            style="text-align:center; border: 1px solid black">
                                <tr>
                                    <th>IMEI</th>
                                    <th>Label</th>
                                    <th>Device Type</th>
                                    <th>Group</th>
                                    <th>Subgroup</th>
                                    <th>Description</th>
                                    <th>Display</th>
                                </tr>    
                            </thead>
                            
                            <tbody>
                                {% for device in devices %}
                                    <tr>
                                        <td>{{device.imei}}</td>
                                        <td>{{device.label}}</td>
                                        <td style="text-transform:uppercase">{{device.device_type}}</td>
                                        <td>{{device.main_group}}</td>
                                        <td>{{device.subgroup}}</td>
                                        <td>{{device.description}}</td>
                                        <td style="text-align:center">
                                            <a href="{% url 'device-detail' device.id %}">i    </a>
                                            <input type="checkbox" id="{{device.imei}}" name="chk" 
                                            value="{{device.imei}}" onclick="show_info_icon()" 
                                            class="chckvalues"/>
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                            <input type="button" class="btn btn-outline-info" onclick='selects()' 
                            value="Select All"/>  
                            <input type="button" class="btn btn-outline-info" onclick='deSelect()' 
                            value="Deselect All"/>                            
                            <button type="submit" class="btn btn-outline-info">Show on Map</button>
                    </form>    
                </div>
j1dl9f46

j1dl9f461#

我认为您最好使用基于函数的视图和典型的“if request.method = POST”逻辑,这并不是通用列表视图的真正用途。

@loginrequired
def device_list_view(request):
    context = {}
    if request.method = POST:
        form = SelectForm(request.POST)
        if form.is_valid():
            # if a request was posted and is valid, do your thing:
            # maybe your thing is to give your map view the device id
            # and render it
            # assuming your form has a field called device_id:
            context['device_id'] = form.device_id 
            return render(request, 'tracking/device_map.html', context)
   
    # either request method is not post or the form wasn't valid
    user_devices = Device.objects.filter(who_added=request.user)
    device_forms = []
    for i, device in enumerate(user_devices):
        form = SelectForm(instance=device, prefix=i)
        device_forms.append(form)
   context['device_forms'] = device_forms
  return render(request, 'tracking/home.html', context)

这里的关键部分是检查请求是否为POST的if-else逻辑,您需要根据实际需要对其进行调整。听起来您实际上是在创建一个表单,从中提取多个device_id。这将帮助您决定是以那种方式还是以您正在尝试的一种形式来完成任务。

相关问题