New to Django

147 views
Skip to first unread message

Wolf Painter

unread,
Aug 16, 2016, 12:22:28 AM8/16/16
to Django users
Hey everyone, I didn't see any posts (or google search) that could possibly answer my question, so I am here... I'm trying to create a way to launch vm's on a Xen or VMWare server using a website. I have the python file written on the Server and am currently creating a DJango app to manage everything. I'm stuck in a big way. I  currently have models that track servers, images and child images launched from the parent, but I can't figure out how to display it properly in a template so i can update the count (either add or subtract a number of VM's from a server). I'm new to both Django and Python, so please excuse any ignorance here... Here are my relevant models:

class Image(models.Model):
name = models.CharField(verbose_name='Image Name', max_length=50)
labID = models.ManyToManyField(Lab, blank=True, related_name='labID')
type = models.CharField(max_length=50)

def __str__(self):
return self.name


class Server(models.Model):
name = models.CharField(verbose_name='Server Name', max_length=50)
ipAddress = models.CharField(max_length=15)
maxCCU = models.IntegerField(default=0)
images = models.ManyToManyField(Image, blank=True, related_name='baseImage')

def __str__(self):
return self.ipAddress

class Childimage(models.Model):
name = models.CharField(verbose_name='Child Image Name', max_length=50)
parent = models.ForeignKey(Image, related_name='base')
server = models.ForeignKey(Server, related_name='server')
inUse = models.BooleanField()
rdpportnum = models.CharField(max_length=4)

def __str__(self):
return self.name

What I'm trying to do is have a web page that displays the number of parent images on a server by counting the child image. For example, I have a parent image called win2k12_excel and there are 12 child images of the parent win2k12_excel on the x.x.x.x server. I would like a web page that shows there are 12 win2k12_excel images on that server that would allow me to add or subtract the number on that server.

I have created a view that does this and puts it into a multidimensional dictionary, but I cannot figure out how to get that to render properly in a template. Here is my view:

def servers(request):
serverlist = Server.objects.all()
imagelist = Image.objects.filter(baseImage__in=serverlist).distinct()

childimagelist = defaultdict(list)
for server in serverlist:
for image in imagelist:
number = Childimage.objects.filter(parent__name__contains=image).filter(server__ipAddress__contains=server).count()
childimagelist[server].append({image: number})
childimagelist.default_factory = None
context = {
"server_list": serverlist,
"image_list": imagelist,
"child_list": childimagelist
}
return render(request, "administration/servers.html", context)

No matter what I have tried so far, I cannot get this dictionary to render in a template. Is there a better way to do what I'm trying to do? Is there a proper way to get the dictionary parsed in the template? Any help would be appreciated. 

Oh, and I have search Google for weeks and tried all the suggestions I found. None of them worked. I can get the first part of the dictionary parsed, but no further than that. Here is the template code (server works, but for image, number in images.items, does not):

{% for server, images in child_list.items %}
<h1>{{ server }}</h1>
{% for image, number in images.items %}
<h1>{{ number }}</h1>
{% endfor %}
{% endfor %}


Any help at all would be appreciated...

Thanks,
Wolf

Wolf Painter

unread,
Aug 16, 2016, 10:58:19 PM8/16/16
to Django users
Anyone have any ideas? I'm really stuck here...

Luis Zárate

unread,
Aug 17, 2016, 2:01:31 AM8/17/16
to django...@googlegroups.com
Hi,

Try to implement a custom tag that can print
Any dictionary in the form that you want.

https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/





El martes, 16 de agosto de 2016, Wolf Painter <wo...@dorkbrigade.com> escribió:
> Anyone have any ideas? I'm really stuck here...
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dee8a26d-adee-4567-84a0-fccd4de478be%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Andrew Beales

unread,
Aug 17, 2016, 6:00:09 AM8/17/16
to Django users
Hi, you can use aggregation queries to count the child images connected to each server:


# views.py

from django.db.models import Count

def servers(request):
    servers
= Server.objects.all()
   
for server in servers:
        server
.child_images = server.images.aggregate(total=Count('base'))
   
return render(request, "administration/servers.html", {'servers': servers})

# template

{% for server in servers %}

 
<h1>Server: {{ server }}</h1>

   
No. images: {{ server.images.count }}<br/>
   
No. child images: {{ server.child_images.total }}

{% endfor %}

https://docs.djangoproject.com/en/1.10/topics/db/aggregation/

('child_image' would be a more suitable related_name than 'base')
 
Also, if server--image is a ManytoMany relation and an image can have many child images, the ForeignKey relation here between Childimage and Server would currently seem redundant/inaccurate.

More broadly, if all child images are types of images, you could consider making ChildImage a subclass of Image instead, ie. class ChildImage(Image): which might help with querying, as then you could just count child images with server.childimage_set.count(), (if you removed the current related_name for Childimage.server).

Andrew Beales

unread,
Aug 17, 2016, 6:19:25 AM8/17/16
to Django users
All this talk about "child images on servers" has probably set off an alarm somewhere.

小柯

unread,
Aug 17, 2016, 11:05:38 AM8/17/16
to Django users
I think it's like answer??
Just create the dictionary like data = {'a': [ [1, 2] ], 'b': [ [3, 4] ],'c':[ [5,6]] }
and then do get it from view

Wolf Painter

unread,
Aug 17, 2016, 2:53:40 PM8/17/16
to Django users
Thank you, I will have to look into this. Django is a bit confusing to me as I'm just learning it, so I'm not as versed as I could be about all the options. What I'm trying to accomplish is a list of the copies of base images on each server, not sure if that makes sense. But here is an example:

win2k12_excel is a base image that can live on many servers, a copy of this image would be 0012_win2k12_excel which would live on only one server. I need to know how many copies of this base image live on each server and display that so I can either increase or decrease the count on each server. Does that make sense? I'm not sure if making ChildImage a subclass is what I want because the number of copies on each server can and will change frequently, as will the number 0012 in the earlier example. On top of the number of copies changing, images can and will be retired, so they will eventually be deleted and servers will change as they need updating. 

I will look into aggregate to see if it will work for what I'm trying to accomplish. Thank you!

Derek

unread,
Aug 18, 2016, 1:33:58 PM8/18/16
to Django users
In your view you have:


    context = {
        "server_list": serverlist,
        "image_list": imagelist,
        "child_list": childimagelist
    }
   
So the variables being passed through to your template are named: server_list, image_list, child_list.

In your template you call this:


    for server, images in child_list.items
 
Which is fine, but then you call this:


    for image, number in images.items

But there is no variable called "images".... I think it should be "image_list:.

HTH
Derek

Wolf Painter

unread,
Aug 20, 2016, 5:23:21 PM8/20/16
to Django users
If you look at the code for the template, images is in there. I've tried all the ways I found to display my multidimensional dictionary, one of which is to loop through the first part, then the second. If you look at the code below, I'm looping through the first part of the dictionary, then the second by calling the value on child_list.items which is images. So far, nothing I've tried is working no matter how I loop through it in the template. I'm still stuck on this as nothing I have looked up has worked, but I am new to django, so there may be something I'm missing.:

{% for server, images in child_list.items %}
<h1>{{ server }}</h1>
{% for image, number in images.items %}
<h1>{{ number }}</h1>
{% endfor %}
{% endfor %}

Sergiy Khohlov

unread,
Aug 20, 2016, 6:29:02 PM8/20/16
to django-users

I've created project for managing virtual machines via libvirt using django few years ago. Also you can take a look at ganeti project. I'm ready to answer any question related to django and cloud computing.

Thanks, Serge


20 серп. 2016 20:23 "Wolf Painter" <wo...@dorkbrigade.com> пише:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Andrew Beales

unread,
Aug 20, 2016, 7:30:26 PM8/20/16
to Django users
This works for me:

{% for server, images in child_list.items %}
   
<h1>{{ server }}</h1>

   
{% for image in images %}
     
{% for key,val in image.items %}
         
<h1>{{ key }}: {{ val }}</h1>

     
{% endfor %}
   
{% endfor %}
{% endfor %}

In your template, 'image' is a list of dicts, so you need to loop over that to get at them.



On Tuesday, August 16, 2016 at 1:22:28 AM UTC+1, Wolf Painter wrote:

Wolf Painter

unread,
Aug 21, 2016, 3:13:59 AM8/21/16
to Django users
OMG THANK YOU!! This totally works! Thank you, Thank you, Thank you!

Wolf Painter

unread,
Aug 21, 2016, 3:15:34 AM8/21/16
to Django users
Hi Serge, I would love to speak to you about your app and how you use it. I'm new to django, so this has taken a bit to get used to but I really think it's the way to go for what I want to do with it. What's the best way to reach you?


On Saturday, August 20, 2016 at 11:29:02 AM UTC-7, Sergiy Khohlov wrote:

I've created project for managing virtual machines via libvirt using django few years ago. Also you can take a look at ganeti project. I'm ready to answer any question related to django and cloud computing.

Thanks, Serge

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages