Passing two tables in

22 views
Skip to first unread message

alam....@gmail.com

unread,
Feb 4, 2017, 9:35:05 AM2/4/17
to Django users
Dear Friends

I have to display data from two independent database tables on the same template page (energy.html). Kindly help..

Table names are "nrg" and "tLoad"

urls:
******************************************************************************************************************************************************************************************************************
url(r'^energy/$', ListView.as_view(queryset=nrg.objects.all().order_by("n_ID")[:25], template_name ='energy.html')),
# url(r'^energy/$', ListView.as_view(queryset=nrg.objects.all().order_by("n_ID")[:25], queryset=tLoad.objects.all().order_by("tL_ID")[:25], template_name ='energy.html')),
******************************************************************************************************************************************************************************************************************



views:
******************************************************************************************************************************************************************************************************************

def refresh(request):
print "Refreshing"
#form1 = STEG()
form2 = refresh()

if request.POST:
query_results1 = nrg.objects.all()
query_results2 = tLoad.objects.all()
print query_results2
return render(request, 'energy.html', {'form1': query_results1}, {'form2': query_results2})
******************************************************************************************************************************************************************************************************************


Table names are "nrg" and "tLoad"
models:
******************************************************************************************************************************************************************************************************************

class nrg(models.Model):
n_ID = models.TextField()#CharField(max_length = 75) 
Code = models.TextField()
Source = models.TextField()
Voltage = models.TextField()
Load = models.TextField()
Battery = models.TextField()
Status = models.TextField()
Remarks = models.TextField()


class tLoad(models.Model):
tL_ID = models.TextField()#CharField(max_length = 75) 
Code = models.TextField()
Load = models.TextField()
Power = models.TextField()
Duration = models.TextField()
Saving = models.TextField()
Status = models.TextField()
Remarks = models.TextField()

******************************************************************************************************************************************************************************************************************




TEMPLATE - 'energy.html':
******************************************************************************************************************************************************************************************************************
            {% block content %} 
            {% for nrg in object_list %}
              <tbody>
                <tr>
                  <td>{{ nrg.n_ID }}</td>
                  <td>{{ nrg.Code }}</td>
                  <td>{{ nrg.Source }}</td>
                  <td>{{ nrg.Voltage }}</td>
                  <td>{{ nrg.Load }}</td>
                  <td>{{ nrg.Battery }}</td>
                  <td>{{ nrg.Status }}</td>
                  <td>{{ nrg.Remarks }}</td>
                </tr>
              </tbody>
              {% endfor %} {% endblock %}

            {% block content1 %} 
            {% for tLoad in object_list %}
              <tbody>
                <tr>
                  <td>{{ tLoad.tL_ID }}</td>
                  <td>{{ tLoad.Code }}</td>
                  <td>{{ tLoad.Load }}</td>
                  <td>{{ tLoad.Power }}</td>
                  <td>{{ tLoad.Duration }}</td>
                  <td>{{ tLoad.Saving }}</td>
                  <td>{{ tLoad.Status }}</td>
                  <td>{{ tLoad.Remarks }}</td>
                </tr>
              </tbody>
              {% endfor %} {% endblock %}  
******************************************************************************************************************************************************************************************************************

ludovic coues

unread,
Feb 5, 2017, 9:07:51 AM2/5/17
to django...@googlegroups.com
ListView is made to display objects from one type of view.

If you want a quick and dirty hack, make one view for nrg, one view
for tLoad and one view with two iframe, each displaying one of the two
other view.

A cleaner solution might need more code. You can look at the ListView
code for inspiration.
> --
> 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/b87030dd-c4d2-42d0-8c6b-0127e43afb88%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
Reply all
Reply to author
Forward
0 new messages