django-templates

Django template access to nested data

This seems silly, but I don't understand how Django Templates access nested data in Contexts. I can access the values of dictionaries nested in the context data structure with the . notation -- {{ aDictionary.i_am_a_key }} works fine. But if I try to iterate over a list of keys and get their value from that same dictionary, I get nothi...

Where to put a Django template's dependent files?

My Django templates use a lot of related stuff: images, style sheets, etc. Where should I put these file, or how should I refer to them in the template itself? For now I'm using the development server. I know it's a really common thing, but I can't really figure it out. ...

Django: Passing argument to parent template

Hello everyone I have templates of this style project - main_templates (including nav bar) -- app1 --- app1_base_template --- app1_templates -- app2 --- app2_base_template --- app2_templates So when rendering, app2_templates extends app2_base_template which extends main_template. What I need to do, is have the corresponding nav ite...

Associate and display additional data with each form row in a Django formset

I'd like to be able to display additional information like a text label with each row in a Django formset. Example/usecase: User picks 5 rows in a model that would he would like some action performed. A popup appears that displays a 5 form formset some additional information (that are model instance methods) based on the 5 rows chosen...

Django Template Slice - Reversing Order

Thanks to a very helpful hint from another question I learned I can limit the amount of values in a list by slicing it in the template as such: {% for comment in thread.comment_set.all|slice:":3" %} Now I would like to get the last 3 results of my comments so I figured a simple ":-3" or "-3" would do the trick, alas: Caught an except...

Nested blocks in Django templates

The master template in my Django app looks like this: {% block parent %} Some text... {% block child %} Default content here... {% endblock child %} ...some more text {% endblock parent %} Now, this template should be overwritten in a way that the child block is changed: {% extends "master.html" %} {% block c...

Literals in django template language?

If I want some text to appear literally in a Django template, e.g. {{Image.jpg|title}} and I want that text to be output (not interpretated) in the HTML, how do I do so? ...

Django template and Python dictionary data structure question

I am trying to present a dictionary from my view.py at the HTML template such as: test = { 'works': True, 'this fails':False } and in the template: This works without a problem: {{ test.works }} But a dictionary key that is having an empty space between words such as 'this fails' doesn't work: {{ test.this fails }} I get this e...

Django general template controled by which variables?

I have been developing some Django app and there's some duplicated code for different Models. I'd like to create a generic table template and pass the Model class, a list of model instances, and Form classes to it so it can render the page and generate the forms to add/delete elements. Then create some generic add/delete views to work wi...

Translate values dict-like in Django templates

In my Model I define choices along the lines of: LANG_CHOICES = ( ("Englisch", ( (u"en-gb", u"England"), (u"en-us", u"United States of America"), ), ) The field is defined as: lang_source = models.CharField(max_length=5, choices=LANG_CHOICES, default="en-gb") Naturally, in my template I'd want to display the...

Bind shorter var names in django template

To the risk of being closed as duplicate (I can't really find it...) I have the following question. I am sure it's either under my nose, or not possible, but I prefer to ask. Is it possible to bind to multiple, shorter variable names in django? I know the existence of with, but with assumes you open a block. I would like to bind three o...

Is there a better way to debug NoReverseMatch in Django templates

If you change your urls.py or you stupidly write some template code that refers to a URL that ultimately doesn't reverse to a URL you get the infamous: NoReverseMatch: Reverse for '<name>' with arguments 'xxx' and keyword arguments '{xxx}' not found. Is there a way to have the line in the template file included in the error? ...

Django Templates: Wordwrap and indent block text

I'm a bit compulsive about having clean, readable and perfectly indented HTML. With Django’s template engine I almost have been able to achieve this with the exception of block text, or rather HTML generated by TinyMCE through the admin panel. The closest I could get is using the wordwrap filter; however this tag does not take any argume...

Nested dot lookups in Django templates

According to The Django Book, Django's templating system supports nested dot lookups: Dot lookups can be nested multiple levels deep. For instance, the following example uses {{ person.name.upper }}, which translates into a dictionary lookup (person['name']), then a method call (upper()): '{{ person.name.upper }} is {{ person.age }}...

Django 1.1 date-based generic view problem - archive_year, archive_month, archive_day

I'm on my first Django blog and when trying to get the posts by year, month and day, using the built-in generic view from Django, but I don't get proper results. (Sorry for my non-professional first question.. if someone knows what is the appropriate question, please let me know) Well, I think it's better to show you my configuration to...

Django: Pagination with urls.py

I'm building a Blog in Django (using Generic Views) and I use the same template for both my date based and list detail views. I'm trying to setup pagination, but I want to do so with URL patterns rather than using an ugly ?page=1 url suffix. The problem is in the actual html template, I cannot find a way to determine which view was used...

Django Dynamic menu design question

Hi, I want to create dynamic menus according to user permissions. As was already discussed here and by the the documentation itself, I know that I can achieve this in the templates using the following snippet: {% if perms.polls.can_vote %} <li> <a href="/polls/vote">Vote</a> </li> {% endif %} But the problem is that f...

python add a new div every 3rd iteration

Hello, I have a product list that put 3 products on a row and clears the row and adds another 3, this works fine everywhere but IE6, i know that adding <div> around each group of 3 products will solve this is the template file at the moment {% for product in category.products.all %} <div class="{% cycle 'clear' '' '' %}"> <a ...

In Django, How do I get escaped html in HttpResponse ?

The following code in one of my views returns unescaped html string which cannot be parsed in frontend since it is an Ajax request. return render_to_response(template_name, { 'form': form, redirect_field_name: redirect_to, 'site': current_site, 'site_name': current_site.name, }, context_instance=Reque...

render_to_response gives TemplateDoesNotExist

I am obtaining the path of template using paymenthtml = os.path.join(os.path.dirname(__file__), 'template\\payment.html') and calling it in another application where paymenthtml gets copied to payment_template return render_to_response(self.payment_template, self.context, RequestContext(self.request)) But I get error Templ...