电脑硬盘坏了重构项目,models.py的设备类如下
class Device(models.Model): device_type = models.CharField(max_length=20, blank=True, null=True) onu_code = models.IntegerField(blank=True, null=True) onu_sn = models.IntegerField(blank=True, null=True) onu_kuandai_vlan = models.CharField(max_length=20, blank=True, null=True) onu_management_ip = models.CharField(max_length=20, blank=True, null=True) onu_management_gateway = models.CharField(max_length=20, blank=True, null=True) onu_EID_code = models.CharField(max_length=20, blank=True, null=True) onu_voice_vlan = models.CharField(max_length=20, blank=True, null=True) onu_voice_ip = models.CharField(max_length=20, blank=True, null=True) onu_voice_gateway = models.CharField(max_length=20, blank=True, null=True) onu_voidport_num = models.CharField(max_length=20, blank=True, null=True) onu_kuandaiport_num = models.CharField(max_length=20, blank=True, null=True) onu_ipmask = models.CharField(max_length=20, blank=True, null=True) onu_voidmask = models.CharField(max_length=20, blank=True, null=True) olt_ontid = models.CharField(max_length=20, blank=True, null=True) olt_management_ip = models.CharField(max_length=20, blank=True, null=True) olt_mac = models.CharField(max_length=20, blank=True, null=True) olt_frame = models.CharField(max_length=20, blank=True, null=True) olt_slot = models.CharField(max_length=20, blank=True, null=True) olt_port = models.CharField(max_length=20, blank=True, null=True) olt_outer_vlan = models.CharField(max_length=20, blank=True, null=True) def __str__(self): return f" {self.device_type} - {self.onu_code or self.olt_management_ip}"
forms.py表单类如下
class DeviceForm(forms.ModelForm): class Meta: model = Device fields = '__all__' # You can specify the fields you want to include here
urls.py涉及部分如下
path('devices/', login_required(views.device_list), name='device_list'), # 设备列表 path('device/<int:device_id>/edit/', login_required(views.edit_device), name='edit_device'), path('device/<int:device_id>/delete/', login_required(views.delete_device), name='delete_device'), path('device/create/', login_required(views.create_device), name='create_device'),
视图函数如下
def device_list(request): devices = Device.objects.all() print(devices) search_query = request.GET.get('search_query', '') if search_query: devices = devices.filter( Q(onu_code__icontains=search_query) | Q(onu_sn__icontains=search_query) | Q(onu_management_ip__icontains=search_query) ) devices = devices.order_by('-id') paginator = Paginator(devices, 20) page_number = request.GET.get('page') devices_page = paginator.get_page(page_number) context = { 'devices_page': devices_page, 'search_query': search_query, } return render(request, 'device_list.html', context)
测试过有返回数据,且其他功能正常
{% extends 'base.html' %} {% block content %} <div class="container mt-5"> <h1 class="mb-4">设备列表</h1> <div class="d-flex justify-content-between align-items-center mb-3"> <div> <a href="{% url 'create_device' %}" class="btn btn-success">添加设备</a> <a href="{% url 'import_devices_excel' %}" class="btn btn-info ml-2">导入设备</a> </div> </div> <table class="table"> <thead> <tr> <th scope="col">ID</th> <th scope="col">设备类型</th> <th scope="col">ONU编码</th> <th scope="col">SN码</th> <th scope="col">ONU宽带vlan</th> <th scope="col">ONU管理IP</th> <th scope="col">ONU管理网关</th> <th scope="col">ONU编码EID</th> <th scope="col">onu语音VLAN</th> <th scope="col">ONU语音ip</th> <th scope="col">ONU语音网关</th> <th scope="col">ONU语音端口数</th> <th scope="col">ONU宽带端口数</th> <th scope="col">ONUIP掩码</th> <th scope="col">ONU语音掩码</th> <th scope="col">oltontid</th> <th scope="col">olt管理地址</th> <th scope="col">oltMAC</th> <th scope="col">olt框</th> <th scope="col">olt槽位</th> <th scope="col">olt端口</th> <th scope="col">oltVLAN</th> </tr> </thead> <tbody> {% for device in devices %} <tr> <td>{{ device.id }}</td> <td>{{ device.device_type }}</td> <td>{{ device.onu_code }}</td> <td>{{ device.onu_sn }}</td> <td>{{ device.onu_kuandai_vlan }}</td> <td>{{ device.onu_management_ip }}</td> <td>{{ device.onu_management_gateway }}</td> <td>{{ device.onu_EID_code }}</td> <td>{{ device.onu_voice_vlan }}</td> <td>{{ device.onu_voice_ip }}</td> <td>{{ device.onu_voice_gateway }}</td> <td>{{ device.onu_voidport_num }}</td> <td>{{ device.onu_kuandaiport_num }}</td> <td>{{ device.onu_ipmask }}</td> <td>{{ device.onu_voidmask }}</td> <td>{{ device.olt_ontid }}</td> <td>{{ device.olt_management_ip }}</td> <td>{{ device.olt_mac }}</td> <td>{{ device.olt_frame }}</td> <td>{{ device.olt_slot }}</td> <td>{{ device.olt_port }}</td> <td>{{ device.olt_outer_vlan }}</td> <td> <a href="{% url 'generate_script' device_id=device.id %}" class="btn btn-light btn-sm">脚本</a> <a href="{% url 'edit_device' device_id=device.id %}" class="btn btn-secondary btn-sm">编辑</a> <a href="{% url 'delete_device' device_id=device.id %}" class="btn btn-danger btn-sm">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> {% endblock %}
页面html文件如下,为什么获取不到遍历的数据,忙了快一天了,试过通过单独一个id能获取到信息,但是就这个不行,求指导