Get first item of QuerySet in template
Not any shorter, but you could use first: {% with entry.image_set.all|first as image %} <img src=”https://stackoverflow.com/questions/3520554/{{ image.get_absolute_url }}”> {% endwith %}
Not any shorter, but you could use first: {% with entry.image_set.all|first as image %} <img src=”https://stackoverflow.com/questions/3520554/{{ image.get_absolute_url }}”> {% endwith %}
request.user is User model object. You cannot access request object in template if you do not pass request explicitly. If you want access user object from template, you should pass it to template or use RequestContext.
Just get rid of the parentheses: {% for company in category.company_set.all %} Here’s the appropriate documentation. You can call methods that take 0 parameters this way.
Use readonly_fields: class MyInline(admin.TabularInline): model = MyModel readonly_fields = [‘link’] def link(self, obj): url = reverse(…) return mark_safe(“<a href=”https://stackoverflow.com/questions/9919780/%s”>edit</a>” % url) # the following is necessary if ‘link’ method is also used in list_display link.allow_tags = True
I’ve had a lot of success with the isoformat function in python: var selected_date = new Date(“{{ selected_date.isoformat }}”)
You haven’t made a mistake – template blocks are included regardless of any conditionals around them. You can see this from this line of the ExtendsNode class of django/template/loader_tags.py in the Django source code: self.blocks = {n.name: n for n in nodelist.get_nodes_by_type(BlockNode)]} When the {% extends %} is being rendered, it fetches all block nodes … Read more
I’m not using language prefixes, but translated urls instead. However, this template tag should also help you: # This Python file uses the following encoding: utf-8 from django import template from django.core.urlresolvers import reverse # from django.urls for Django >= 2.0 from django.core.urlresolvers import resolve # from django.urls for Django >= 2.0 from django.utils import … Read more
You need to add the RequestContext in your render_to_response for the context processors to be processed. In your case: from django.template.context import RequestContext context = {‘latest’: p} render_to_response(‘Question/latest.html’, context_instance=RequestContext(request, context)) From the docs: context_instance The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with … Read more
You’ll want to pass the currently selected org into the view, maybe as current_org so that when you’re iterating through the orgs you can compare with the current one to determine whether or not to select it, like: {% for org in organisation %} <option value=”{{org.id}}” {% if org == current_org %}selected=”selected”{% endif %}> {{org.name|capfirst}} … Read more
I use a custom template tag for another js templating system, here: https://gist.github.com/629508 Use in template: {% load mytags %} {% verbatim %} {{ This won’t be touched by {% django’s %} template system }} {% endverbatim %} Edit: This custom template tag is no longer necessary, as Django’s template language now supports the {% … Read more