class Store(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
class Phones(models.Model):
phone = models.CharField(max_length=50)
store = models.ForeignKey(Store)
from django.shortcuts import render
from .models import Store, Phones
def get_store():
stores = []
for line in Store.objects.all():
phone = line.phones_set.filter(show=1)
l = []
l.append(line)
l.append(phone)
stores.append(l)
return stores
def index(request):
template = 'myhomechita/page.html'
context = { 'stores': get_store,}
return render(request, template, context)
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for block in stores %}
<li>
{% for unit in block %}
{% if forloop.counter == 1 %}
<span>{{ unit.name }} - {{ unit.address }}</span>
{% else %}
<ul>
{% for phone in unit %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% if stores_queryset %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores_queryset %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
{% for phone in store. phones_set %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
class Store(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
@property
def showed_phones(self):
return self.phones_set.filter(show=1)
'stores': Store.objects.all()
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores_queryset %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
{% for phone in store.phones_set %}
<li>{{ phone.name }}: {{ phone.phone }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
class Phones(models.Model):
phone = models.CharField(max_length=50)
store = models.ForeignKey(Store, related_name='phone_number')
store = Store.objects.get(pk=1)
phone_num = store.phone_number.phone
{% if stores %}
<div class="footer-col-2">
<div class="col-name">Наши магазины</div>
<ul class='stores'>
{% for store in stores %}
<li>
<span>{{ store.name }} - {{ store.address }}</span>
<ul>
<li>{{ store.phone_number.name }}: {{ store.phone_number.phone }}</li>
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endif %}