django-templates

Getting a list of errors in a Django form

I'm trying to create a form in Django. That works and all, but I want all the errors to be at the top of the form, not next to each field that has the error. I tried looping over form.errors, but it only showed the name of the field that had an error, not an error message such as "Name is required." This is pretty much what I'd like to ...

Test for existence of template block in a template

I have a structure where there's normally a page heading in (% block heading %} in my base template: base.html <h2>{% block heading %}{% endblock %}</h2> Most of the time, I'll pass in a heading like this through templates that extend base: extends-base.html {% block heading %}Super Cool Page!{% endblock %} However, for a special...

Testing Django model choices in templates

Say I have a model like: from django.db import models USER_TYPE_CHOICES = ( (1, 'Free'), (2, 'Paid'), ) class Account(models.Model): name = models.CharField(max_length=20) user_type = models.IntegerField(default=1, choices=TYPE_CHOICES) and in a template I want to test the user_type to show a special section if the u...

Komodo Edit 5.2 Django Template Syntax Error - Info: <head> previously mentioned

I am using Komodo Edit 5.2 for editing html and Django template files. It always shows a single syntax error inside the first {% block %} area on the first tag of my template. For example: {% extends "base.html" %} {% load i18n %} {% block title %}Hello{% endblock %} {% block content %} <p>Hello</p> <-- Syntax error on this singl...

How to try reusable django apps that don't provide templates inside?

Many of the reusable django apps don't include default templates inside. James Bennett says in his presentation Reusable Apps that providing truly portable default templates is very hard. He adds that most of the bug reports in the first release of his registration app were related to default templates. This is a valid point. But withou...

Email templating in django

As we all know (or should), you can use Django's template system to render email bodies: def email(email, subject, template, context): from django.core.mail import send_mail from django.template import loader, Context send_mail(subject, loader.get_template(template).render(Context(context)), '[email protected]', [email,]) T...

accessing foreignKey inside the template

well it's quiet simple. 2 models with ManyToMany relation: class Artist(models.Model): name = models.CharField(max_length=100, unique=True) slug = models.SlugField(max_length=100, unique=True, help_text='Uniq value for artist page URL, created from name') birth_name = models.CharField(max_length=100, blank=True) c...

Dynamic direct_to_template

In my webapp, there are a lot of errors or other messages that just show a template that is very close to the URL. At the moment, I have half a dozen static mappers like this: (r'^/message/foo/$', 'direct_to_template', {'template': 'message/foo.html'}), (r'^/message/bar/$', 'direct_to_template', {'template': 'message/bar.html'}), Is t...

Django templating: wrap first n words of paragraph in tag

Using the standard Django templating system, is there any snippet/reusable template tag to have the first n words in a piece of text wrapped in a tag so I can style them? What I'm ideally looking for: {{item.description|wrap:"3 span big"}} which outputs: <span class="big">Lorem ipsum dolor</span> sit amet, consectetur adipiscing e...

How do I get all the variables defined in a Django template?

I'm new to Django and I wonder if there is a way to dump all the variables available to a template for debugging purposes. In Python I might use something like locals(), is there something equivalent for the default template engine? Note: suppose I don't have access to the view for the purposes of this question. ...

Accessing a dict by variable in Django templates?

My view code looks basically like this: context = Context() context['my_dict'] = {'a': 4, 'b': 8, 'c', 15, 'd': 16, 'e': 23, 'f': 42 } context['my_list'] = ['d', 'f', 'e', 'b', 'c', 'a'] And what I'd like to do in my Django template is this: <ul> {% for item in my_list %} <li>{{ item }} : {{ my_dict.item }}</li> {% endfor %} </u...

Accessing parallel arrays in Django templates?

My view code looks basically like this: context = Context() context['some_values'] = ['a', 'b', 'c', 'd', 'e', 'f'] context['other_values'] = [4, 8, 15, 16, 23, 42] I would like my template code to look like this: {% for some in some_values %} {% with index as forloop.counter0 %} {{ some }} : {{ other_values.index }} <br/> ...

How to modify the rendering of a wdget in admin template?

I am trying to overrride the "admin/includes/fieldset.html" template. I am rendering "Group", a ModelMultipleChoiceField field as a CheckboxSelectMultiple widget. However I want to change the display depending on the choices selected. {% if field.is_checkbox %} {{ field.field }}{{ field.label_tag }} {% else %} ...

Speeding up templates in GAE-Py by aggregating RPC calls

Here's my problem: class City(Model): name = StringProperty() class Author(Model): name = StringProperty() city = ReferenceProperty(City) class Post(Model): author = ReferenceProperty(Author) content = StringProperty() The code isn't important... its this django template: {% for post in posts %} <div>{{post.content}}</di...

Django TemplateSyntaxError: too many values to unpack

I'm working with a django form, and I have a choice field. I think the problem may be that the choices are fetched dynamically, and right now there's only one value. I'm getting the TemplateSyntaxError: too many values to unpack. Some of the other posts seem to say that having only one value is a problem, so i adjusted my function that f...

What method/property in a django field is called from the template?

I want to subclass models.TextField so I can return the text to the template with occurences of \r\n replaced with <br />. I don't want the <br />'s stored in the database, however. What method is called to retrieve the field's data when invoked from the template? I tried this, but it doesn't seem to work: class HTMLTextField(models.Te...

FileField Size and Name in Template

How do I get the size and name of a FileField in a template? My model is setup like this: class PDFUpload(models.Model): user = models.ForeignKey(User, editable=False) desc = models.CharField(max_length=255) file = models.FileField(upload_to=upload_pdf) My template is setup like this: {% for download in downloads %} ...

django access related user model value in template

Hi, I have a model looking like this. class ProjectMembership(models.Model): member = models.ForeignKey(User, related_name='project_membership_member_set') Edit: In a template I want now to access the last_name of the User model. I thought it should work like the following line, but it does not. {{ project_membership.member.last...

Django: Why does this custom model field not behave as expected?

The following field is meant to format money as a two places decimal (quantized). You can see that it returns a <decimal>.quantize(TWOPLACES) version of the stored decimal. When I view this in the Django admin however, it doesn't do that. If I put in 50 to a field that is using CurrencyField() and view it in the admin, I get 50 vs 50.00....

Django: extra variables on a custom 404 template

Hi all, I want to be able to use extra variables on a custom 404 template. #404.html {{ extra_var }} I have already tried: #urls.py from myproject.myapp import views handler404 = views.handler404 #views.py from django.template import RequestContext, loader from django import http def handler404(request): extra_var = 'my_extra_v...