I have a project with some components that should probably be separated out into reusable apps. However, all of the templates extend a couple of base templates for the entire site. Should I include copies of the base template in each app's template directory? What is the best way to structure template directories/inheritance?
For the purposes of your app, base.html or whatever the base template is called, should contain the entire HTML document so that it can function on its own. If end-users want to override the template, they can add their own copy to templates/yourappname/base.html and do whatever they want inside that file, including extend their main site template.
Related
Django 3.0.6
For one model I need a modified admin site template.
Namely, I want to modify this template:
admin/includes/fieldset.html
I have copied the fieldset.html from Django package directory and placed it like this:
/my_project/image/templates/admin/includes/fieldset.html
Here image is my application. It is this application that needs a modified admin template.
The problem is that all other models also get this template. And the used template filters don't receive necessary params and explode.
Documentation: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#set-up-your-projects-admin-template-directories
Well, I got confused and fail to organize the necessary directories structure.
How can I use the modified template only for one Image app?
You have a couple of issues. Firstly, only the following templates can be overridden per-app or per-model:
actions.html
app_index.html
change_form.html
change_form_object_tools.html
change_list.html
change_list_object_tools.html
change_list_results.html
date_hierarchy.html
delete_confirmation.html
object_history.html
pagination.html
popup_response.html
prepopulated_fields_js.html
search_form.html
submit_line.html
fieldset.html isn't in there, so you'll need to see if the including template(s) is in this list, and if it is, replace this template and use it to include your own version of fieldset.html.
Also, your location (slightly modified since can't override included templates per-app): /my_project/image/templates/admin/template.html isn't quite right. This will overwrite that template for every app. To do it per-app, you need a further subdirectory under admin:
/my_project/image/templates/admin/image/template.html
The reason for this is that templates don't really care about which app they're in, so the fact that the template lives in your image app doesn't mean anything to Django, the convention of putting them in your app's sub-directory is solely to avoid overriding templates that you don't intend to.
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.
To utilize django's own provided templates like forgot password, change password and such templates and views funcionality, I made my own templates for login e.t.c. But after some time I found that there are many views and templates already present that one can utilize just by providing right URL but only problem is that these templates are extending django's own template. So I want to know that is there a way to only set extended template of my own choice that is my site's main template so that I just give the URL of forgot password or change password e.t.c. related pages and django render the template by extending my main template instead of django's own template. I am not sure if admin panel is extending the same template that other pages are.
Please tell how can I do so and also please tell if doing so can have any problem.
More detail:
I know inheritance and extending with our template file but how can I set my own template in such way that they automatically inherit mine.
For example, for forgotpassword, I had to make a template file and need to paste form elements and top of it I write {%extends main.html%} but I don't want that, I know that default forgot password template is being inherited from some default django template I want to set it some so that it always inherit from my template in that case.
Short answer: You have to write your own templates.
Longer answer:
Django does include some templates, but these are all intended solely for the admin application. The auth views default to using 'registration/some_template.html', and those templates all live in the admin app ( see https://github.com/django/django/tree/master/django/contrib/admin/templates/registration). The auth views all support a template keyword argument that you can supply in your URLConf, so that you can specify which of your own templates to use.
Think of the admin templates as a reference that you can use when writing your own templates, but not as something you're meant to use directly.
Try to get your head around template inheritance. I think you can achieve what you want using template inheritance. This is a good post introducing the topic.
New to Django. In the Django docs, I know it says "Custom template tags and filters must live inside a Django app.". I am going to have 4 or 5 apps, each with custom template tags (some of which will be the same tags as those in other apps). Even though it says that, is it possible to make one master folder for all of my template tags at the root of my project? It seems like having separate folders for each app violates D.R.Y....
Well, there's no reason to have duplicate tags; a template tag library in any app can be loaded from any template. You should only have one copy of any tag.
That said, what's commonly done is to create a template tag APP to house all template tags.
Just build a blank application called tags, utils, whatever, and put all template tags in that app.
root/utils/templatetags
It doesn't even need a model.py/urls.py to function in installed_apps.
You could make an app thats only purpose is to handle some general things like template tags or utils. Sometimes I do this and just call it "app".
But even if you keep things the way they are, there is no reason to duplicate template tags because they are available in all templates anyway. Unless you plan on breaking the apps up and using them in different projects, just put the template tag in the appropriate app, and load it from a template.
When particularly extend template and when to use include ? Is include of any use with content like user profile section (like about me in the corner of our site) ?
Extending allows you to replace blocks (e.g. "content") from a parent template instead of including parts to build the page (e.g. "header" and "footer"). This allows you to have a single template containing your complete layout and you only "insert" the content of the other template by replacing a block.
If the user profile is used on all pages, you'd probably want to put it in your base template which is extended by others or include it into the base template. If you wanted the user profile only on very few pages, you could also include it in those templates.
If the user profile is the same except on a few pages, put it in your base template inside a block which can then be replaced in those templates which want a different profile.
See about django template inheretance.
Extends sort of 'includes' the parent template and then can overwrite parts of it for different functionality.
Include does a simple include rendering a template in a current context.
extends creates "parent child relation". There is a chance of over-writing of parent functionality in case of extends. While include simply renders the html response.
Using % include ... with see docs allows to override variables from included page. So could not agree with muhammad-awais-bin-majid answer.
Suppose these two clauses just represent different ways of the pages constructing:
extends - to use like an external wrapper for the page,
include - to insert included page inside the current one.
Also one can use several extending pages just in chained nesting,
but including allow us to use several included pages one by one and not only in chained nesting.