I have the following codes:
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} )
All my request.POST.get are successful and there is data in each. When i press the save button, the print(variable)
are printed correctly in my powershell. But I cant seem to save.
My web shows me the error of
'WSGIRequest' object has no attribute 'get'
I tried the following:
The first solution was adding ,''
behind the html name. So it becomes request.POST.get('html name' , '')
. But this didnt change the output or solve the error. It still show the same error when i was using request.POST.get('html name')
. The 2nd solution was request.GET.get
instead of request.POST.get
but this give me the same error
Here is the traceback from powershell:
Traceback (most recent call last):
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Intern\django_swing\interface\views.py", line 747, in device_port_selected
job.save()
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\forms\models.py", line 459, in save
if self.errors:
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\forms\forms.py", line 170, in errors
self.full_clean()
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\forms\forms.py", line 372, in full_clean
self._clean_fields()
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\forms\forms.py", line 384, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "C:\Users\P1338475\.virtualenvs\django_swing-t91g66f4\lib\site-packages\django\forms\widgets.py", line 263, in value_from_datadict
return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'