models py class Catalog models Model catalog_name models CharField max

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#models.py
class Catalog(models.Model):
catalog_name = models.CharField(max_length = 50, verbose_name="Имя каталога")
catalog_slug = models.SlugField(max_length=50, verbose_name="Служебное поле")
subcatalog = models.ManyToManyField(Subcatalog, null=True, verbose_name="Подкаталоги", blank=True)
class Meta:
verbose_name_plural = "Каталоги"
verbose_name = "каталог"
def __unicode__(self):
return self.catalog_name
def get_absolute_url(self):
return "/catalog/"+self.slug
#vievs.py
@render_to('index.html')
def index(request):
return {'catalogs': Catalog.objects.all(),
'top': Product.objects.filter(in_top=True),
'user':request.user,}
index.html
{% block left %}
<ul>
{% for i in catalogs %}
<li><a href="{{i.catalog_name.get_absolute_url}}">{{i.catalog_name}}</a></li>
{% ifequal i.catalog_name catalog_url %}
<ul>
{% for q in subcatalog_urls %}
<li><a href="/catalog/{{i.catalog_name}}/{{q.subcatalog_name}}">{{q.subcatalog_name}}</a></li>
{%endfor%}
</ul>
{% endifequal %}
{%endfor%}
</ul>
{% endblock %}