我有以下觀點:
我有以下觀點。
def device_port(request):
設備 = Device.objects.all()
如果request.method == "POST":
selected=request.POST.get('device')
devices = Device.objects.get(pk=selected)
tablename = 'dev_interface_' selected
print("tablename: " tablename)
cursor=connection.cursor()
cursor.execute(f "SELECT interface FROM {tablename} WHERE id >=2" )
righttable = cursor.fetchall()
return redirect('/device/port/selected',{'devices':devices, 'selected': selected, 'righttable':righttable} )
return render(request, 'interface/device_port.html',{'devices':devices})
def device_port_selected(request, pk):
如果request.method == "POST":
job = JobForm(request.POST)
device = devices.hostname
print(devices)
#job.relateddevice = devices.hostname
嘗試一下。
selection=request.POST.get('portrange')
除了。
selection = ""
messages.warning(request, "Please select the ports")
print(selection)
#job.associatedinterface = selection
return render(request, 'interface/device/port/selected/' device '.html',{'devices':devices, 'righttable':righttable} )
return render(request, 'interface/device_port_selected.html',{'device':device, 'selected': selected, 'righttable':righttable} )
urls.py
urlpatterns = [
path('', views.home, name='interface-home')。
path('device/', DeviceListView.as_view(), name='interface-device')。
path('device_edit/<int:pk>/', views.device_edit, name='device-edit')。
path('device_delete/<int:pk>/', views.device_delete, name = 'device-delete'),
path('device_add/', views.device_add, name='device-add')。
path('device/port/', views.device_port, name='device-port')。
path('device/port/selected/', views.device_port_selected, name='device-port-selected')。
path('device/routeport/', views.device_routeport, name='device-routeport')。
path('interface/', views.interface_list, name='interface-list')
]
device_port.html
<form method="POST">
<div class="form-row align-items-center"/span>>
<div class="col-md-5 my-1"/span>>
{% csrf_token %}
<label for="Hostname"/span>> Hostname</label>
<div class="input-group">/span>
< select id = "list" class="custom-select mr-sm-2" onchange="getSelectValue(); ">
<option selected>Select</option>/span>
{% for device in devices %}
<option value={{device. id}}>{{device.hostname}}</option>
{%endfor%}
</select>{%endfor%}}。
<div class="input-group-append"/span>>
< 按鈕 class="btn btn-outline-secondary" type="submit"> 去</按鈕>
</div>/span>
</div>/span>
</div>/span>
</div>/span>
< input type ="text" name ="device" id="txtvalues" style=display: none">
</form>/span>
所以我在這里處理了兩個頁面(/device/port和/device/port/selected)。在第一頁/device/port中,用戶需要從下拉框中選擇一個值,并按下Go按鈕。從這里開始,它打算進入下一個頁面,即/device/port/selected,第一頁中選擇的值將傳遞到下一頁。
但是用下面的代碼,我收到的錯誤是
。device_port_selected() missing 1 required positional argument: 'pk'
當從第一頁移動到下一頁時。
uj5u.com熱心網友回復:
你不能向redirect傳遞一個背景關系字典。第二個引數應該是URL引數,而不是一個背景關系。因此,改變以下行:
return redirect('/device/port/selected',{'device':devices, 'selected': selected, 'righttable': righttable} )
to
return redirect('device-port-selected', pk=selected)
注意,最好使用URL的名稱(即device-port-selected)而不是整個路徑,因為你可以在未來改變路徑而不影響你的其他代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323534.html
標籤:
