Django Python - How to display informations from different tables in one HTML template -
i have mysql database containing 4 tables. tables have 4 corresponding classes in models.py file (in django app). have defined views in views.py file , made html templates each template can communicate 1 view (and think has this). display on html template informations coming different tables (classes models.py).
for example: have template "about.html" this:
{% load static %} <link rel="stylesheet" type="text/css" href="{% static 'pdbapp/stat_style.css' %}" /> <body class="news"> <header> <div class="nav"> <ul> <li class="home"><a href="/pdbapp/home/">home</a></li> <li class="info pdb"><a href="/pdbapp/pdbinfo/">infos</a></li> <li class="about"><a class="active" href="/pdbapp/about/">about</a></li> </ul> </div> </header> </body> <h1>about us</h1> <h2>project members</h2> {% if namelist %} <ul> {% in namelist %} <li><a href="/pdbapp/pdbinfo/{{ }}">{{ }}</a></li> {% endfor %} </ul> {% else %} <p>no informations available.</p> {% endif %}
and display in html template number of entries in mysql tables "pdb" , "struct_sec" (see below):
create table pdb( id_pdb_chain char(5) not null primary key, id_pdb char(4) not null, chaine varchar(10) not null, header varchar(255) not null, sequence_proteine text not null, start_seq int not null, taille_proteine int not null, resolution_pdb float not null, meth_res varchar(10) not null, foreign key (meth_res) references methodes_res(meth_res) on delete cascade ); create table struct_sec( id_struct_sec int not null auto_increment primary key, start_pred int not null, structure_predite text not null, nombre_ppii int not null, pourcentage_ppii float not null, angle_phi text not null, angle_psi text not null, id_pdb_chain char(5) not null, nom_analyse varchar(7) not null, foreign key (id_pdb_chain) references pdb(id_pdb_chain) on delete cascade, foreign key (nom_analyse) references methodes_analyse(nom_analyse) on delete cascade );
here corresponding class in file "models.py":
from __future__ import unicode_literals django.db import models class pdb(models.model): id_pdb_chain = models.charfield(db_column='id_pdb_chain', primary_key=true, max_length=5) # field name made lowercase. id_pdb = models.charfield(db_column='id_pdb', max_length=4) # field name made lowercase. chaine = models.charfield(max_length=10) header = models.charfield(max_length=255) sequence_proteine = models.textfield(db_column='sequence_proteine') # field name made lowercase. start_seq = models.integerfield() taille_proteine = models.integerfield(db_column='taille_proteine') # field name made lowercase. resolution_pdb = models.floatfield(db_column='resolution_pdb') # field name made lowercase. meth_res = models.foreignkey('methodesres', models.do_nothing, db_column='meth_res') # field name made lowercase. def __unicode__(self): return self.id_pdb class meta: managed = false db_table = 'pdb' class structsec(models.model): id_struct_sec = models.autofield(primary_key=true) start_pred = models.integerfield() structure_predite = models.textfield(db_column='structure_predite') # field name made lowercase. nombre_ppii = models.integerfield(db_column='nombre_ppii') # field name made lowercase. pourcentage_ppii = models.floatfield(db_column='pourcentage_ppii') # field name made lowercase. angle_phi = models.textfield() angle_psi = models.textfield() id_pdb_chain = models.foreignkey(pdb, models.do_nothing, db_column='id_pdb_chain') # field name made lowercase. nom_analyse = models.foreignkey(methodesanalyse, models.do_nothing, db_column='nom_analyse') # field name made lowercase. def __str__(self): return str(self.id_pdb_chain) class meta: managed = false db_table = 'struct_sec'
how should this. beginner in django, please believe tried understand has been explained on web unsuccessfully.
your view should this:
def detail(request, poll_id): pdbs = pdb.objects.all() structsecs = structsec.objects.all() return render(request, 'detail.html', {'pdbs': pdbs, 'structsecs': structsecs})
then in template:
{% pdb in pdbs %} {{pdb.header}} {% endfor %} {% structsec in structsecs %} {{structsecs.structure_predite}} {% endfor %}
Comments
Post a Comment