Django查询集从数据库获取数据?
我创建了一个模型,如下所示:Django查询集从数据库获取数据?,django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,我创建了一个模型,如下所示: from __future__ import unicode_literals from django.db import models class TypesOfVehicle(models.Model): type = models.CharField(max_length=50) def __unicode__(self): return self.type class vehicleDetails (models.
from __future__ import unicode_literals
from django.db import models
class TypesOfVehicle(models.Model):
type = models.CharField(max_length=50)
def __unicode__(self):
return self.type
class vehicleDetails (models.Model):
T = models.ForeignKey(TypesOfVehicle)
NoOfWhl = models.PositiveIntegerField()
year = models.CharField(max_length=4)
ModelName = models.CharField(max_length=254)
VID = models.CharField(max_length=254, verbose_name="VID")
为了查看上述数据,我写下了如下视图:
from django.shortcuts import render
from .models import CountryDiseases, Country
def VData(request):
Count = vehicleDetails.objects.all()
return render(request, 'DATAPLO/MAP.html', {'Count': Count })
为了呈现视图,我写下了一个简单的模板
MAP.html
{% for c in Count %}
{{c.NoOfWhl }} {{ c.year }} {{ c.ModelName }}<br/>
{% endfor %}
现在让我们假设,如果有人点击“SUV”,它将返回与“SUV”相关的所有信息,如下所示:
URL键为“SUV”:
我试图理解您的问题,我将用Django web应用程序中的一个示例来回答 在我的例子中,但你的情况类似,我有一个模板,它将我数据库中的所有公司呈现在一个表中。我显示所有公司,如果我在单元格中单击,我可以根据该公司的所有信息查询新模板。这和你要求的问题完全一样 第一步:url.py文件 在此文件中,根据您的问题,我有3个URL:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^Formulaire/Societes$', views.Identity_Societe_Form, name = "SocieteFormulaire"),
url(r'^Resume/Societes$', views.Identity_Societe_Resume, name = "SocieteResume"),
url(r'^Contrat/Societe/(?P<id>\d+)/$', views.Identity_Contrat, name="Contrat"),
]
我在这个名为:Identity\u Societe\u Resume.html的html模板中显示在我的数据库中注册的所有公司
此模板的核心如下所示:
<h4><b> <span class="glyphicon glyphicon-user"></span></span> Récapitulatif des Sociétés ayant souscrit à un contrat de services : </b></h4>
<table style="width:125%">
<tbody>
<tr>
<th>ID</th>
<th>Nom</th>
<th>État</th>
<th>SIRET</th>
<th>SIREN</th>
<th>NAF-APE</th>
<th>Adresse</th>
<th>Ville</th>
<th>Pays</th>
</tr>
{% for item in societe %}
<tr>
<td><a href="http://localhost:8000/Identity/Contrat/Societe/{{item.id}}"> Ici </a></td>
<td>{{ item.Nom}}</td>
<td>{{ item.Etat}}</td>
<td>{{ item.SIRET }}</td>
<td>{{ item.SIREN }}</td>
<td>{{ item.NAF_APE }}</td>
<td>{{ item.Adresse }}</td>
<td>{{ item.Ville}}</td>
<td>{{ item.Pays.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
拥有:(请求,id)
非常重要,在我提到的每个查询集中,关于公司id
最后,在我的模板中,我有:
<h4><b> Récapitulatif concernant la société : {{societe.Nom}}</b></h4>
<table style="width:125%">
<tbody>
<tr>
<th>ID</th>
<th>Nom</th>
<th>État</th>
<th>SIRET</th>
<th>SIREN</th>
<th>NAF-APE</th>
<th>Adresse</th>
<th>Ville</th>
<th>Pays</th>
</tr>
<tr>
<td>{{societe.id}}</td>
<td>{{ societe.Nom}}</td>
<td>{{ societe.Etat}}</td>
<td>{{ societe.SIRET }}</td>
<td>{{ societe.SIREN }}</td>
<td>{{ societe.NAF_APE }}</td>
<td>{{ societe.Adresse }}</td>
<td>{{ societe.Ville}}</td>
<td>{{ societe.Pays.name }}</td>
</tr>
</tbody>
</table>
Réconcernant la societété投降:{{{societe.Nom}
身份证件
笔名
埃塔
汽笛
警报器
NAF-APE
阿迪斯
维尔
支付
{{societe.id}
{{societe.Nom}}
{{societe.Etat}
{{societe.SIRET}
{{societe.SIREN}}
{{societe.NAF_APE}}
{{法国兴业银行}
{{societe.Ville}}
{{societe.Pays.name}
希望这个例子对我有帮助,我为我的英语很糟糕而道歉。我的天啊,这是一个大而完整的回答谢谢:)因为我正在做我的项目,我没有太多时间来调整我的脚本以适应他的问题。但从我的角度来看,这正是他想要的:)谢谢你这么长的答案和代码,尽管我是初学者,因此我需要根据我的要求来适应这些代码。将在试用此代码数小时后返回。感谢您在回答中或评论框中添加几行关于“paginator=paginator()”的内容,这些内容与您的代码相关,因为在阅读了很多之后,我不太确定“paginator”到底能为我们做些什么。提前感谢paginator
让我们用页面进程显示表格。如果您有一个20行的表格,您可以设置分页进程,将显示内容分为两页,每页10个元素,或5页,每页4个元素,以此类推。。如果显示包含多行的html表,则有必要使用此选项;)如果我的回答解决了你的问题,你能接受吗
@login_required
def Identity_Societe_Resume(request) :
societe = Societe.objects.all()
contrat = SocieteContrat.objects.all()
paginator = Paginator(societe, 10)
page = request.GET.get('page', 1)
try:
societe = paginator.page(page)
except PageNotAnInteger:
societe = paginator.page(1)
except EmptyPage:
societe = paginator.page(paginator.num_pages)
paginator = Paginator(contrat, 10)
page = request.GET.get('page', 1)
try:
contrat = paginator.page(page)
except PageNotAnInteger:
contrat = paginator.page(1)
except EmptyPage:
contrat = paginator.page(paginator.num_pages)
context={
"societe" : societe,
"PageNotAnInteger":PageNotAnInteger,
"contrat" : contrat,
}
return render(request, 'Identity_Societe_Resume.html', context)
<h4><b> <span class="glyphicon glyphicon-user"></span></span> Récapitulatif des Sociétés ayant souscrit à un contrat de services : </b></h4>
<table style="width:125%">
<tbody>
<tr>
<th>ID</th>
<th>Nom</th>
<th>État</th>
<th>SIRET</th>
<th>SIREN</th>
<th>NAF-APE</th>
<th>Adresse</th>
<th>Ville</th>
<th>Pays</th>
</tr>
{% for item in societe %}
<tr>
<td><a href="http://localhost:8000/Identity/Contrat/Societe/{{item.id}}"> Ici </a></td>
<td>{{ item.Nom}}</td>
<td>{{ item.Etat}}</td>
<td>{{ item.SIRET }}</td>
<td>{{ item.SIREN }}</td>
<td>{{ item.NAF_APE }}</td>
<td>{{ item.Adresse }}</td>
<td>{{ item.Ville}}</td>
<td>{{ item.Pays.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
@login_required
def Identity_Contrat(request, id) :
societe = get_object_or_404(Societe, pk=id)
contrat = get_object_or_404(SocieteContrat, pk=id)
#etc ...
<h4><b> Récapitulatif concernant la société : {{societe.Nom}}</b></h4>
<table style="width:125%">
<tbody>
<tr>
<th>ID</th>
<th>Nom</th>
<th>État</th>
<th>SIRET</th>
<th>SIREN</th>
<th>NAF-APE</th>
<th>Adresse</th>
<th>Ville</th>
<th>Pays</th>
</tr>
<tr>
<td>{{societe.id}}</td>
<td>{{ societe.Nom}}</td>
<td>{{ societe.Etat}}</td>
<td>{{ societe.SIRET }}</td>
<td>{{ societe.SIREN }}</td>
<td>{{ societe.NAF_APE }}</td>
<td>{{ societe.Adresse }}</td>
<td>{{ societe.Ville}}</td>
<td>{{ societe.Pays.name }}</td>
</tr>
</tbody>
</table>