1

I have a page which is for album/picture management with 2 sections: Albums and Pictures.

When an album is selected the pictures block needs to change via AJAX to reflect the album selected.

This means the rendered pictures block needs to be provided to the Albums page as well as be available as it's own View for the AJAX source.

I understand I could solve this by making the pictures block always render from AJAX even when the album page loads, however I would like to deliver the default album pictures within the initial page load if possible. In order to do that, I'd like to render the pictures block via the same template in the Album page view as is used for the Picture AJAX View.

I'm only familiar with providing templates as a template_name property within an TemplateView object.

I guess I could simply call an instance of PictureView using inclusion_tag, and pull the data I need out of the render_to_response (I haven't tried this yet, I'm just theorizing) however that seems a bit dirty to me. I'm wondering if there's a more elegant solution to this problem?

Thanks!

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
DanH
  • 5,498
  • 4
  • 49
  • 72

2 Answers2

2

jQuery, Django templates, JS templating and backbone.js should tie this together.

I would suggest having a dedicated template for the Pages block. Include this template in the Django template for your page.

For dynamic updates use a JS templating library such as included in underscore.js or moustache.js. You can change the template demlimiters used so that they are the same as djangos.

Include the raw Pages block template into a javascript template block - use the django ssi tag.

Use django-tastypie to set up an api that returns the data for Photos as JSON. Your template library can then use data to fill in the template in the JS template block and you can then replace the Photo block with this rendered HTML. Use of backbone.js should ease the tying of DOM elements and JS events.

Community
  • 1
  • 1
scytale
  • 12,346
  • 3
  • 32
  • 46
  • and it looks like Django 1.5 will have a {% verbatim %} template tag to help with just this situation: https://docs.djangoproject.com/en/dev/releases/1.5/#verbatim-template-tag – scytale Oct 12 '12 at 10:49
  • While you're answer provided me with a good read, sienf's answer was ultimately what I was looking for, and a bit more lightweight :) – DanH Oct 24 '12 at 08:30
1

If I understand your question correctly, I did something similar once with the subsection having its own template file, which only describes that one section of the page. I used the include tag to include it into the page template, so it loaded when the page did and then rendered that template with updated values and replaced it on the page with AJAX when the content was meant to change.

Is that an option for you?

bdeniker
  • 995
  • 1
  • 9
  • 21
  • This is what I settled on actually, not sure why I didn't consider it initially, probably just my stupid brain again! Thanks :) – DanH Oct 24 '12 at 08:30
  • No worries, glad I could help. The other answer is also very good, but it is a lot more effort for a greater benefit. – bdeniker Oct 31 '12 at 12:13