django-templates

Resetting cache for Django cached template loader

。_饼干妹妹 提交于 2019-12-04 19:30:24
问题 Django 1.2 introduced a new template loader, that stores data in cache ( django.template.loaders.cached.Loader ). Unfortunatly, i failed to find any info on how the cache is invalidated and when and how it is reset. I want to use this on my server, but i'm not sure, that it would reset on django restart (that would be enough for me). 回答1: By digging into django's source, you could find the template loaders for current server instance are stored at django.template.loader.template_source

TypeError object is not iterable

最后都变了- 提交于 2019-12-04 19:25:48
问题 I am getting the following error when trying to loop over a variable in my Django templates. The variable in question is the related object of the model specified in my DetailView subclass: TypeError at /en/applicants/50771459778/ 'Householdmember' object is not iterable Here is my models.py file: class Applicant(models.Model): user = models.ForeignKey(User, editable=False) bank_card_number = models.CharField(_('Bank card number'),max_length=50, unique=True) site_of_interview = models

django template and dictionary of lists

好久不见. 提交于 2019-12-04 19:11:44
问题 I'm using django's template system, and I'm having the following problem: I pass a dictionary object, example_dictionary, to the template: example_dictionary = {key1 : [value11,value12]} and I want to do the following: {% for key in example_dictionary %} // stuff here (1) {% for value in example_dictionary.key %} // more stuff here (2) {% endfor %} {% endfor %} However, this does not enter on the second for loop. Indeed, if I put {{ key }} on the (1), it shows the correct key, however, {{

django 'if' statement improperly formatted

吃可爱长大的小学妹 提交于 2019-12-04 18:59:54
Im getting strangest error in django so far: 'if' statement improperly formatted Template that raises the error is this: {% if diff >= 0 %} <span class="pos">+{{ diff }} {% else %} <span class="neg">-{{ diff }} {% endif %} </span> <span>{{ a }}</span> view that has a and diff in context is this: def add(request, kaart_id): if request.is_ajax() and request.method == 'POST': x = Kaart.objects.get(id=kaart_id) x.pos += 1 x.save x = Kaart.objects.get(id=kaart_id) from django.utils import simplejson diff = x.pos - x.neg a = "(+1)" context = { 'diff':diff, 'a':a } return render_to_response('sum.html

Django Templates: Display a date only if it's in the future

妖精的绣舞 提交于 2019-12-04 18:50:24
How do I compare dates in a Django template? I thought of several possible avenues: Send today's date in the context to be compared at the template Create my own template tag Have some logic in the view to only pass the date if it's in the future Although the last option seems easier, I rather leave the display logic in the template than in the view. I also do not want to pass something that seems so trivial like today's date in the template context. Perhaps someone has another option or could share their implementation of one of the options I mentioned above and why they decided to go that

Make django templates strict

痴心易碎 提交于 2019-12-04 18:37:30
问题 In a django template, a call to {{ var }} will silently fail if var is undefined. That makes templates hard to debug. Is there a setting I can switch so django will throw an exception in this case? The only hint at a solution I've found online is http://groups.google.com/group/google-appengine/browse_thread/thread/86a5b12ff868038d and that sounds awfully hacky. 回答1: Django<=1.9 Set TEMPLATE_STRING_IF_INVALID = 'DEBUG WARNING: undefined template variable [%s] not found' in your settings.py .

Django: List Products of each Categories in a page

▼魔方 西西 提交于 2019-12-04 18:17:24
I have a few categories and I would like to list the products per category in the format below (categories is an FK to products): Category 1 bunch of products .... Category N bunch of products I have tried many ways but so far I only get the categories but not the products to show in my HTML. models.py class Category(models.Model): title = models.CharField(max_length=225) slug = models.SlugField(unique=True, blank=True, null=True) def __str__(self): return self.title def get_absolute_url(self): return reverse('category_detail', kwargs={'slug': self.slug}) @property def get_products(self):

Django string concatenation inside template tag best practice

穿精又带淫゛_ 提交于 2019-12-04 18:14:31
问题 I'm trying to concatenate some strings to format a URL inside my template tag, but I don't find an elegant way. So far, what I have is: {% button "Activate" "http://" site.domain url 'registration_activate' activation_key %} Is there any best practice to make it a bit more "readable"? Thanks a lot 回答1: You can concatenate two strings in Django template as follows: {{"First String "|add:"Second String"}} Just replace the two strings with your own variable. 回答2: What I use when I want to

django how to loop through the context object passed back by a generic detailview?

試著忘記壹切 提交于 2019-12-04 18:14:07
I'm using a generic DetailView to display a project object. Can I loop through the fields somehow in my template or do I have to place every field. url(r'^(?P<slug>[-\w]+)/$', DetailView.as_view(model=Project, template_name='projects/detail_project.html',slug_field='slug', context_object_name='project'), name='project_detail'), I've got something like this in my template: {{ project.title }} {{ project.created_date }} etc... Is there a way to do something like this? <table> {% for field in project %} <tr> <td>{{ field }}</td> </tr> {% endfor %} </table> I tried the above snippet and got this

use slugify in template

我是研究僧i 提交于 2019-12-04 18:12:02
问题 I want to have SEO-friendly URL ,my current url in urls.py : (ur'^company/news/(?P<news_title>.*)/(?P<news_id>\d+)/$','CompanyHub.views.getNews') I use it in template: {% for n in news %} <a href="{% url CompanyHub.views.getNews n.title,n.pk %}" >{{n.description}}</a> {% endfor %} I use news_id to get news object with that PK . I want to convert this url: ../company/news/tile of news,with comma/11 to: ../company/news/tile-of-news-with-comma/11 by doing some thing like this in template: {% for