WSGIRequest'对象没有属性'get'

2024-10-04 05:26:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

models.py

class Job(models.Model):
    datetime = models.DateTimeField(default=timezone.now)
    associateddevice = models.CharField(max_length = 50)
    associatedinterface = models.CharField(max_length = 500)
    combinedparameters = models.CharField(max_length = 500)

forms.py

class JobForm(ModelForm):
    class Meta:
        model = Job
        fields = ['associateddevice', 'associatedinterface','combinedparameters']

views.py

def device_port_selected(request, pk):
    
    devices = Device.objects.get(pk=pk)        
    tablename = 'dev_interface_'+str(pk)
    #print("tablename: " +tablename)
    cursor=connection.cursor()
    cursor.execute(f"SELECT interface FROM {tablename} WHERE id >=2")
    righttable = cursor.fetchall()
    cursor.close()
    #print(righttable)     
    if request.method == "POST":
        job = JobForm(request)        
        hostname = devices.hostname
        #print(device)
        
        try:
            selection=request.POST.get('portrange','')
        except:
            selection = ""       
        print(selection)     
        mode=request.POST.get('portmode','')        
        print(mode)        
        status=request.POST.get('portstatus','')
        print(status)
        portpara1 = request.POST.get('portpara1','')
        print(portpara1)
        portpara2 = request.POST.get('portpara2','')
        print(portpara2)
        #combined=mode + ";" +status + ";" + portpara1 + ";" + portpara2
        #print(combined)
        combined={'port_range':selection, 'port_mode':mode, 'port_status':status, 'port_param1':portpara1, 'port_param2': portpara2} 
        combinedfinal = {'device':hostname, 'configuration':combined}
        print(combinedfinal)
        job.combinedparameters=combinedfinal
        print(type(request.POST), '\n', request.POST)

    
        job.save()
       
        

        return redirect('/device/', {'device':Device.objects.all, 'devices':device})
        
    return render(request, 'interface/device_port_selected.html',{'devices':devices, 'righttable':righttable} )

我所有的request.POST.get都成功了,每个都有数据。当我按下保存按钮时,print(variable)会在我的powershell中正确打印。但我似乎无法拯救。 我的网站向我展示了

'WSGIRequest' object has no attribute 'get'

我提到了其他人以前的帖子:'WSGIRequest' object has no attribute 'get' 但提供的前2个解决方案不起作用。第一个解决方案是在html名称后面添加,''。所以它变成了request.POST.get('html name' , '')。但这并没有改变输出或解决错误。当我使用request.POST.get('html name')时,它仍然显示相同的错误。 第二个解决方案是request.GET.get而不是request.POST.get,但这给了我同样的错误

print(type(request.POST), '\n', request.POST)的输出

<class 'django.http.request.QueryDict'>
 <QueryDict: {'csrfmiddlewaretoken': ['ihYDX9Ug1ybl8aIKLI6kcaAjHvc7A6oIeUTadKbZcSJZURjGSWejiip7u9vJs7fQ'], 'portmode': ['Access'], 'port_param1': ['1\r\n1\r\n1'], 'portpara1': ['1\\n1\\n1'], 'portstatus': ['Enabled'], 'port_param2': ['1\r\n1\r\n1'], 'portpara2': ['1\\n1\\n1']}>

以下是powershell的回溯: enter image description here

html中的回溯: ![image|690x331](upload://mPLfkXRsNtSty4UzTBiyxwWSkhN.png)


Tags: getportmodemodelsrequestdevicehtmlstatus