我有以下3种型号:
class Apartament(models.Model):
Nume = models.CharField(max_length=200)
Proprietar = models.ForeignKey(User, on_delete=models.CASCADE)
Judet = models.CharField(max_length=200)
Oras = models.CharField(max_length=200)
Strada = models.CharField(max_length=200)
pub_date = models.DateTimeField('Data inregistrare')
def __str__(self):
return self.Nume
class Provideri(models.Model):
Nume = models.CharField(max_length=200)
def __str__(self):
return self.Nume
class Facturi(models.Model):
Numar = models.CharField(max_length=200)
Serie = models.CharField(max_length=200)
Apartament = models.ForeignKey(Apartament, on_delete=models.CASCADE, default='0')
Provider = models.ForeignKey(Provideri, on_delete=models.CASCADE, default='0')
Suma = models.DecimalField('Suma de plata', max_digits=5, decimal_places=2)
DataEmitere = models.DateField('Data emitere')
DataScadenta = models.DateField('Data scadenta')
DetaliiFactura = models.TextField('Detalii Factura', help_text="Ce reprezinta factura?")
def __str__(self):
return "{} - {} {}".format(self.Provider,self.Serie, self.Numar)
此视图:
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Apartament
template_name = 'apartament/detail.html'
这个模板:
{% extends "base.html" %}
{% block content %}
{% if apartament.Proprietar == user %}
<div class="card">
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<div class="card-header">
{% block title %}{{ apartament.Nume }}{% endblock %}
</div>
<div class="card-body">
<div class="row">
<div class="col-lg-10">
{{facturi}}
{% for factura in facturi %}
aaaaaaaaaaa<br>
{% endfor %}
</div>
<div class="col-lg-2">
Judet: {{apartament.Judet}}<br>
Oras: {{apartament.Oras}}<br>
Strada: {{apartament.Strada}}<br>
</div>
</div>
</div>
</div>
{% else %}
<div class="alert alert-warning" role="alert">
<h4 class="alert-heading">Apartamentul nu iti apartine</h4>
<p>
Cel putin in aplicatie :)
</p>
</div>
{% endif %}
{% endblock %}
如何在模板上显示“ Facturi”列表以及请求的Apartament_id中的信息
我尝试使用上面的代码,但是当我在模板上添加{{facturi}}时,没有任何显示
return Facturi.objects.filter(Apartament_id=Apartament.id)
# also with
facturi = Facturi.objects.filter(Apartament_id=Apartament.id)
打印时,它会显示在我的控制台上,而不显示在模板上。
python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
<QuerySet [<Facturi: ENGIE - 123 GAZ>, <Facturi: RDS - 123 RDS>]>
System check identified no issues (0 silenced).
May 27, 2019 - 19:04:10
Django version 2.2.1, using settings 'myHome.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
我想在应用程序上显示包含以下内容的列表:
1. Facturi:ENGIE-123 GAZ
2. Facturi:RDS-123 RDS
来自:<QuerySet [<Facturi: ENGIE - 123 GAZ>, <Facturi: RDS - 123 RDS>]>
已进入控制台
答案 0 :(得分:1)
您需要将facturi
添加到上下文中。将该方法添加到您的视图类:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['facturi'] = Facturi.objects.filter(Apartament=Apartament)
return context
您需要在模板中添加要在迭代过程中显示的必填字段,例如Numar
和Serie
字段:
{% for factura in facturi %}
{{ factura.Numar }}
{{ factura.Serie }}
{% endfor %}
答案 1 :(得分:1)
我认为您需要使用get_context_data将“ Facturi”添加到从视图传递到模板的上下文对象中: https://docs.djangoproject.com/en/2.2/ref/class-based-views/mixins-simple/
是这样的:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Facturi'] = Facturi.objects.filter(Apartament_id=Apartament.id)
return context
然后,您应该可以通过以下方式访问模板中的Facturi查询集:
{% for factura in Facturi %}
{% endfor %}