Найти - Пользователи
Полная версия: Django как передать переменную во все шаболны
Начало » Python для новичков » Django как передать переменную во все шаболны
1
and_07
пример модели
#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()

как передать во все шаблоны метод categories_menu
что бы можно было так написать

				{% for cat in categories.categories_menu %}
{{cat.name}}
{% endfor %}

или может есть другой способ?


and_07
тема закрыта
http://stackoverflow.com/questions/2246725/django-template-context-processors
VadimK
https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/

Имхо правильнее создать свой тэг с шаблоном и его подключать. По крайней мере легче в сопровождении, нет дублей кода по разным темплейтам. И постороннему разработчику легче найти откуда и что взялось.

and_07
VadimK
да ваше решение лучше
and_07
чет не выходит

не выводит
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
тема закрыта
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB