Requirements: On the same page on different plates can be dynamically switched, using a view implementation, as shown, click on the PHY display physical machine list, click VM Display virtual Machine list, PHY, VM corresponding URL is generated dynamically:
Implementation ideas:
1. Create two new tables, the servers table is used to store the server kind, pvserver the physical machine virtual machine specific server information. The physical and virtual machines do not have to be stored in two tables, so that the server list cannot be derived directly from the server type in view
#Storage Server Typeclassservers (models. Model): ServerID= Models. Integerfield (primary_key=true,unique=True) ServerType= Models. Charfield (max_length=20) def __str__(self):returnSelf.servertype#storage of physical and virtual machine server listsclassPvserver (models. Model): ServerType= Models. ForeignKey (servers,to_field='ServerID', db_column='ServerType') #设置外键, associated to the ServerID of the servers table, ensuring a consistent name for the server type = Models. Charfield (max_length=100) def __str__(self):returnSelf.name
2. After synchronizing the two tables to db, register them with admin background for adding server information
from. ModelsImportHv,vm,servers,physerver,vmserver,pvserverclassserversadmin (admin. Modeladmin):#Fields = [' sn ', ' IP ']FieldSets =[None, {' Fields': ['ServerID']}), (None, {' Fields': ['ServerType']}),] List_display= ('ServerID','ServerType')#add more columns.Admin.site.register (servers,serversadmin)classpvserveradmin (admin. Modeladmin):#Fields = [' sn ', ' IP ']FieldSets =[None, {' Fields': ['ServerType']}), (None, {' Fields': ['name']}),] List_display= ('ServerType','name')#add more columns.Admin.site.register (Pvserver,pvserveradmin)
3. New View:
Method One (simple), direct query Pvserver table:
def servers (request,serverid): # Span style= "color: #008000;" >serverid from the following types, in order to save time to use types in this view, in fact, types should be used from another page under the types = Serversm.objects.all () # get all kinds of servers in front-end Web display Serverall = Pvserver.objects.filter (servertype= serverid) context ={ " serverall : Serverall," types :types} return render (Request, aptest/ servers.html , context)
Method Two, query the servers table first, and then query the Pvserver table by the foreign key:
defServers (Request,serverid):#ServerID from the following types, in order to save time to use types in this view, in fact, types should be used from another pageServerList = Serversm.objects.get (Serverid=serverid)#From aptest.models import servers as SERVERSMtypes = Serversm.objects.all ()#Get all the server types on the front-end Web displayServerall = Serverlist.pvserver_set.all ()#traverse the appropriate server list based on the type of servercontext={'Serverall': Serverall,'Types': Types}returnRender (Request,'aptest/servers.html', context)
4. Edit the URL:
URL (r'^servers/(\d+)/$', aptest.servers),
5. Access http://192.168.50.74/aptest/servers/2/can be returned.
Django Plate Dynamic Switching