Django equivalent of Rails application.html.erb? - django

Is there a shared "master" layout in Django for HTML files similar to Rails application.html.erb? If not, is there a best practice on how to go about creating one?

In Django, the best practise is to use three levels of template using template inheritance.
I quote the django book to explain you:
You can use as many levels of inheritance as needed. One common way of
using inheritance is the following three-level approach:
Create a base.html template that holds the main look and feel of
your site. This is the stuff that rarely, if ever, changes.
Create a base_SECTION.html template for each “section” of your site
(e.g., base_photos.html and base_forum.html). These templates
extend base.html and include section-specific styles/design.
Create individual templates for each type of page, such as a forum
page or a photo gallery. These templates extend the
appropriate section template.
This approach maximizes code reuse and makes it easy to add items to
shared areas, such as section-wide navigation.

Related

Django CMS blog - Replacing templates for different cases

I'm not sure, Django CMS Blog is complete app or foundation for self-extending ? I can see one huge logical mistake - inside one app config, I can't easy change template - for list, detail, archive and item - they are strict and hardcoded in plugin code.
Is there any way to customize it without hacking the app/writing tons of custom plugins ?
I need several versions for each template - especially for blog_item.html, blog_detail.html and blog_archive.html.
I want to use plugin feature from Django CMS and don't want to hardcode whole layout in template include logic because this is broking MVC logic (!).
OK, I know, I can include my custom subtemplate blog_item in list template with instance varibles, but I totaly can't imagine, how to do it in several places - front page, list page, post page etc BUT using regular Django CMS plugin features - I have static placeholders and tpl blocks - but there still is one template for item, inside app code.
I understand (less/more) Django template inheritance, but this case is more complicated than simple inheritance between templates.
At frontpage I want different list of latest posts - one template
at category list - different layout - so next template,
inside post - detail_view - need to be customized in other way.
Can somebody guide me, how to achieve this ? Custom plugin for each layout case or something ? A bunch of IF statement in template, dependend from contex is ridiculous..
I have replaced templates set, for app config - configured templates prefix, and whole folder with articles/*.html files. But changing item subtemplate cause change layout part in many places in the same time. This is main problem. Each using "latest posts" plugin, use same template - and I can't set template files for each plugin instance.
After some tests and talks, there is no nice way to replace templates, except extending plugin class and write own better version of plugin...

Dynamic templates with Django and Ajax

My question is a bit generic in that my problem is a broad one. I have been working with django for some time, and I really want to move more into doing very dynamic web pages where actual page reloads aren't common.
I have read about the different popular javascript frameworks available, and I always feel like I am missing part of the puzzle, particularly in templating.
What are some of the best practices for keeping my templating code as non redundant as possible. I get the impression that a lot of templating logic will make it's way into the JS in addition to my django templates. I want to avoid situations where I am writing templating code in two different places.
For a very basic example, let's say I am writing some template code inside Django for an input field that has a set number of attributes. I have then also written in JS that when I click on a button, another input field of the same type is generated with all the appropriate attributes. In practice this could be a form that takes an arbitrary amount of e-mail addresses. The problem I see is that when I want to change something about that input field, I need to do it in two places.
Is there a particular development paradigm or work flow that I am unaware of? How are issues like this generally avoided?
Recommendations on frameworks would be amazing too!
as you mentioned above:
Use Django Template language. Pass the data from view to template dynamically.
Read Django Template Language documentation.
For JS :
its better to write your js in home.html.... use {% include %} tag for other html

Is it possible to make multi-level template inheritance in django templates?

I have three html-files:
base.html
page.html
comment.html
in page.html I extend base.html. In comment.html I extend page.html. Will comment.html extend base.html's blocks?
Yes, you can actually use as many levels of inheritance as you'd like. From The Django Book:
One common way of using inheritance is the following three-level approach:
(1) Create a base.html template that holds the main look-and-feel of your site. This is the stuff that rarely, if ever, changes.
(2) Create a base_SECTION.html template for each “section” of your site. For example, base_photos.html, base_forum.html. These templates
all extend base.html and include section-specific styles/design.
(3) Create individual templates for each type of page, such as a forum page or a photo gallery. These templates extend the appropriate
section template.
More here: http://www.djangobook.com/en/2.0/chapter04.html
Yes, it will. Why not to try it yourself?

Typo3 - Multiple page templates

I am very new to Typo3, and I have a very basic (not to say dumb) question: is it possible to have multiple page templates or can you only have one template?
What I need to do is have different templates call different scripts because apparently having the same template call all of them seems to create conflicts.
Thank you for your help!
Jane
Using TypoScript, you can include any number of scripts in your template.
You can even imagine the following page tree:
-Home
--Page 1
--Page 2
---Page A
---Page B
--Page 3
You can set a certain template for the homepage, and it will apply to all pages. But you can set a completely different template for page 3, and it will only apply to that page. You can also apply a slightly different template to page 2, overriding specific values (page background, CSS inclusion, etc), and it will apply to Page A and Page B as well.
yes, you can have many different templates on a site. Actually each single page can have its very own template. You might be interested in checking out templavoila since its very graphical and once you got it set up, you can easily change between templates within the Typo3 backend.
Tutorials
An alternative would be to use the original TypoScript templates which is not difficult yet requires a bit of TypoScript understanding.
TypoScript Templates
In any way you can chose between different templates.

How can I pass a standard, static variable from all views in Django?

I'm working on a blog application, and I want to have a sidebar that includes a list of all the months the blog has been in existence, to provide links to archives pages. Moreover, I'd like to make this automatically update when the month changes, rather than hardcoding it in the template. Of course, as far as I can tell, this means that I'll have to calculate the list of months in every view, and pass it into every template from every view.
I'd like to avoid this, if possible. Is there a way to calculate the list once and automatically apply it to every template, without having to explicitly pass it into the template from every view?
There are a few possible solutions to your problem.
If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.
An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.
With either solution you should also look at Django's cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.
You want a template context processor
Django - having middleware communicate with views/templates
http://docs.djangoproject.com/en/dev/ref/templates/api/?from=olddocs#id1
Django's template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.
Template Inheritance:
http://www.djangobook.com/en/1.0/chapter04/#s-template-inheritance
A combination of custom template tags as mentioned previously and template fragment caching should do the trick.