Django Plate Dynamic Switching

Source: Internet
Author: User

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)

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 (servers,serversadmin)classpvserveradmin (admin. Modeladmin):#Fields = [' sn ', ' IP ']FieldSets =[None, {' Fields': ['ServerType']}), (None, {' Fields': ['name']}),] List_display= ('ServerType','name')#add more (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 be returned.

Django Plate Dynamic Switching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.