django-custom-tags

Django, custom tag… how?

青春壹個敷衍的年華 提交于 2019-12-23 03:23:07
问题 I would like to make a django custom tag to display 10 entry titles from the category where the user is reading an article. How can I do this? I need to pass the category from the actual entry. 回答1: The best way to do this would be with an inclusion tag. This is a tag that renders a template fragment which renders the 10 related articles. You just pass in the current article into the tag, and return the context for the template fragment - ie the related articles. @register.inclusion_tag(

Is it possible to use django's custom template tags to insert code in other blocks in the template?

跟風遠走 提交于 2019-12-23 02:42:05
问题 I'm writing a custom template tag that wraps an HTML element with some code to make it editable. This is backed up by some CSS, and JS that takes care of sending the data to the server to save it. This component requires the inclusion of <script type="text/javascript" src="../myscript.js"></script> at the bottom of the page and <link rel="stylesheet" type="text/css" href="../mystyle.css"> at the top. I already have two "js" and "css" template blocks in the page template. My question - is

Pass a context variable through an inclusion tag

╄→гoц情女王★ 提交于 2019-12-12 06:32:49
问题 Performing a check to see whether or not a user is attending or not. How do I pass the context variable is_attending to the template without getting a syntax error on 'is_attending': context['is_attending'] ? The check is basically for styling divs and whatnot. What am I doing wrong? template: {% for event in upcoming %} {% registration %} {% if is_attending %} Registered! {% else %} Register button {% endif %} yadda yadda divs... {% endfor %} filters.py @register.inclusion_tag('events/list

Problem loading custom template tags (Error: No module named x)

做~自己de王妃 提交于 2019-12-10 01:55:06
问题 I am currently writing a few custom template tags but for some reason they will not load. My directory structure is as follows: MyProj | ----MyApp | |----templatetags | |----myapp_tags.py |----__init__.py In myapp_tags.py from django.template import Library, Node from myproj.myapp.models import Product register = Library() class LatestProductsNode(Node): def render(self, context): context['recent_products'] = Product.objects.all()[:5] return '' def get_latest_products(parser, token): return

Is it possible to use django's custom template tags to insert code in other blocks in the template?

与世无争的帅哥 提交于 2019-12-06 15:57:52
I'm writing a custom template tag that wraps an HTML element with some code to make it editable. This is backed up by some CSS, and JS that takes care of sending the data to the server to save it. This component requires the inclusion of <script type="text/javascript" src="../myscript.js"></script> at the bottom of the page and <link rel="stylesheet" type="text/css" href="../mystyle.css"> at the top. I already have two "js" and "css" template blocks in the page template. My question - is there a way for for the custom template tag to include these scripts once in the relevant page blocks, if

Problem loading custom template tags (Error: No module named x)

╄→尐↘猪︶ㄣ 提交于 2019-12-05 01:27:02
I am currently writing a few custom template tags but for some reason they will not load. My directory structure is as follows: MyProj | ----MyApp | |----templatetags | |----myapp_tags.py |----__init__.py In myapp_tags.py from django.template import Library, Node from myproj.myapp.models import Product register = Library() class LatestProductsNode(Node): def render(self, context): context['recent_products'] = Product.objects.all()[:5] return '' def get_latest_products(parser, token): return LatestProductsNode() get_latest_products = register.tag(get_latest_products) In settings.py INSTALLED

how to activate DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

不羁的心 提交于 2019-12-05 00:30:46
问题 I read this "DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you'll have to activate it. " from this page http://docs.djangoproject.com/en/dev/ref/templates/api/ But it seems there is no information how to activate this processor. Here is my original question Access request in django custom template tags

Access request in django custom template tags

ぃ、小莉子 提交于 2019-11-26 23:48:42
My code in myapp_extras.py: from django import template register = template.Library() @register.inclusion_tag('new/userinfo.html') def address(): address = request.session['address'] return {'address':address} in 'settings.py': TEMPLATE_CONTEXT_PROCESSORS =( "django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors.request' ) but I got an error: TemplateSyntaxError at /items/ Caught an exception while rendering: global name 'request' is not defined Original

Access request in django custom template tags

爷,独闯天下 提交于 2019-11-26 09:16:17
问题 My code in myapp_extras.py: from django import template register = template.Library() @register.inclusion_tag(\'new/userinfo.html\') def address(): address = request.session[\'address\'] return {\'address\':address} in \'settings.py\': TEMPLATE_CONTEXT_PROCESSORS =( \"django.core.context_processors.auth\", \"django.core.context_processors.debug\", \"django.core.context_processors.i18n\", \"django.core.context_processors.media\", \'django.core.context_processors.request\' ) but I got an error: