Форум сайта python.su
0
пример модели
#model class Categories(models.Model): parent = models.ForeignKey('self',to_field='name', default=None, blank=True, null = True) meta_title = models.TextField(max_length=200,blank=True, null=True) meta_description = models.TextField(max_length=255,blank=True, null=True) meta_keywords = models.TextField(max_length=255,blank=True, null=True) title = models.TextField(max_length=200,blank=True, null=True) url = models.TextField(max_length=200,blank=True, null=True) name = models.TextField(max_length=100,unique=True,blank=True, null=True) visible = models.IntegerField(default=1,null=True) short = models.SmallIntegerField(blank=True, null=True) def parent_name(self): return self.parent.name parent_name.admin_order_field = 'parent__name' def __unicode__(self): return u'%s' % (self.name) def get_absolute_url(self): return "/category/%i/" % self.id def categories_menu(self): return Categories.objects.filter()
{% for cat in categories.categories_menu %}
{{cat.name}}
{% endfor %}Офлайн
0
тема закрыта
http://stackoverflow.com/questions/2246725/django-template-context-processors
Отредактировано and_07 (Фев. 25, 2015 16:19:30)
Офлайн
16
https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/
Имхо правильнее создать свой тэг с шаблоном и его подключать. По крайней мере легче в сопровождении, нет дублей кода по разным темплейтам. И постороннему разработчику легче найти откуда и что взялось.
Офлайн
0
VadimKда ваше решение лучше
Отредактировано and_07 (Фев. 26, 2015 08:23:17)
Офлайн
0
чет не выходит
не выводит
from django import template from test.models import Networks,Categories,Offers register = template.Library() def categories(): categories = Categories.objects.all() return {'categories': categories} register.inclusion_tag('test.html', takes_context=True)(categories)
{% load tagslib %} {% for cat in categories %} {{cat.name}} {% endfor %}
Отредактировано and_07 (Фев. 26, 2015 09:49:24)
Офлайн
0
тема закрыта
Офлайн