Django Template Based If Statement Issue

40 views
Skip to first unread message

G Z

unread,
May 12, 2014, 5:02:34 PM5/12/14
to django...@googlegroups.com
Views.py

from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms

def index(request):
        customers = Customer.objects.all
        vms = Vms.objects.all
        ctx = { 'customers':customers, 'vms':vms}
        return render_to_response('index.html', ctx)

index.html

{% for e in customers %}
        {% for d in vms %}
                {% if e.NAME == d.CUSTOMERID %}
                        <div> {{ e.id }} - {{ e.NAME }} - {{ d.VMNAME }}     </div>
                {% endif %}

                <br><hr>
                <center>Test Data </center>
                <a href='/{{e.id}}'>e.name - {{ e.NAME }}</a><br>
                                d.customerid - {{ d.CUSTOMERID }}

        {% endfor %}
{% endfor %}

The section for test data will display but the section under the iff statement wont and there are values that match exactly.

Tom Evans

unread,
May 12, 2014, 5:42:43 PM5/12/14
to django...@googlegroups.com
Generally people do their table joins in the database rather than the
template...

Could we start with what are you trying to achieve with this page,
rather than what is going wrong. What HTML should that section
generate?

Cheers

Tom

G Z

unread,
May 12, 2014, 5:47:09 PM5/12/14
to django...@googlegroups.com
I'm just trying to associate each vm tied to the customers id and display the vms under the customer that are associated with his id.

G Z

unread,
May 12, 2014, 5:50:53 PM5/12/14
to django...@googlegroups.com
so as you can see when ever the vm field customer id matches the customers name i want to display the vm with the customer. To create a table of vms that each customer has. I also will be associating a third database with vmspecs to each vm.

I know there is an easier way to do this with django but i didn't quite understand waht the documentation was sayign






On Monday, May 12, 2014 3:02:34 PM UTC-6, G Z wrote:

Tom Evans

unread,
May 12, 2014, 6:00:27 PM5/12/14
to django...@googlegroups.com
On Mon, May 12, 2014 at 10:47 PM, G Z <zuk...@gmail.com> wrote:
> I'm just trying to associate each vm tied to the customers id and display
> the vms under the customer that are associated with his id.
>

Normally, you would have some sort of relationship between those
models, and the template code would look something like this:


{% for customer in customers %}
{% for vms in customer.vms_set.all %}
<div>{{ vms.id }} - {{ vms.NAME }} - {{ d.VMNAME }}</div>
<!-- etc -->
{% endfor %}
{% endfor %}

Please note, even if you are using a legacy database, there is no need
for the model's fields to have the same name as the table column. All
caps normally denotes a constant, these are not constants, and
"VMNAME" is less pleasant to look at than "virtual_machine_name" or
even "vm_name".

One other thing, your view, you should be actually calling the
function "all()", not passing the function object "all":

def index(request):
customers = Customer.objects.all()
ctx = { 'customers':customers }
return render_to_response('index.html', ctx)

Note the brackets, and you no longer need to pass in 'vms' either, as
it should come from the relation.

If this doesn't work, please show your models.

Cheers

Tom

G Z

unread,
May 12, 2014, 6:15:08 PM5/12/14
to django...@googlegroups.com, teva...@googlemail.com
this is my models 

from django.db import models

# Create your models here.

class Customer(models.Model):
    NAME = models.CharField(max_length=200)
    WEBSITE = models.CharField(max_length=200)
    PHONE = models.CharField(max_length=200)
    EMAIL = models.CharField(max_length=200)
    ADDRESS = models.CharField(max_length=200)
    VMIDS = models.CharField(max_length=200)

    def __unicode__(self):
        return self.NAME
class Vms(models.Model):
    VMNAME = models.CharField(max_length=200)
    VMSTATUS = models.CharField(max_length=200)
    CUSTOMERID = models.ForeignKey(Customer)

    def __unicode__(self):
        return self.VMNAME
class Vmspecs(models.Model):
    CPUS =  models.CharField(max_length=200)
    CORES =  models.CharField(max_length=200)
    MEMORY =  models.CharField(max_length=200)
    HDSPACE =  models.CharField(max_length=200)
    OS =  models.CharField(max_length=200)
    UPTIME = models.CharField(max_length=200)
    VMID  = models.ForeignKey(Vms)
    CUSTOMERID = models.ForeignKey(Customer)

    def __unicode__(self):
       return unicode(self.VMID)

if im not wrong the foreignkey is the association

G Z

unread,
May 12, 2014, 6:15:54 PM5/12/14
to django...@googlegroups.com, teva...@googlemail.com
also thank you so much for your help.


On Monday, May 12, 2014 4:00:27 PM UTC-6, Tom Evans wrote:

G Z

unread,
May 12, 2014, 6:37:47 PM5/12/14
to django...@googlegroups.com, teva...@googlemail.com
ok its still not putting out the vm data i get 

Grant - google.com - 7029855378 - zuk...@gmail.com - 2332 oakland st
- -


        {% for customer in customers %}
                        <div>{{ customer.NAME }} - {{ customer.WEBSITE }} -  {{customer.PHONE}} - {{ customer.EMAIL }} - {{ customer.ADDRESS }}    </div>
                {% for Vms in customer.vms_set.all %}
                        <div>{{ vms.id }} - {{ vms.NAME }} - {{ vms.VMSTATUS }}</div>
                {% endfor %}
        {% endfor %}


def index(request):
       customers = Customer.objects.all()
       ctx = { 'customers':customers }
       return render_to_response('index.html', ctx)




its still not working correctly.


donarb

unread,
May 12, 2014, 7:19:15 PM5/12/14
to django...@googlegroups.com, teva...@googlemail.com
You have

{% for Vms in customer.vms_set_all %} 

And then 

<div>{{ vms.id }}...

Vms is not the same variable as vms.


Reply all
Reply to author
Forward
0 new messages