I've already configured the necessary things to work the extends template function in django. here's my codes:
in settings.py
def my_dir():
import os.path
return os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = ( my_dir() + '/app/templates', ) #dynamic template directory
in base.html - located in app/templates/site
....
<div id="SideBar" class="FloatLeft">
{% block sidebar %} {% endblock %}
</div>
....
in sidebar.html - located in app/templates/site
{% extends "site/base.html" %}
{% block sidebar %}
some code here
{% endblock %}
I've tried also the {% include "site/sidebar.html"%} tag in the base.html to check the template directory, and yes include tag is working...
what's the problem in the {% extends %} ? why does it doesnt detect its parent template..
please help me guys.. your help is greatly appreciated... im still waiting for the answer.. tnx
Which template are you rendering in your view? It should be the child, not the parent.
I am not sure what yout problem is, but you should check the following points :
The {% extends %} tage should be the first one in the template file (and put a blank line afterwards to be sure)
I think that the reference to the base template is relative to you TEMPLATE_DIR. Try different things like putting both templates at the same level etc.
Check all the tags in both templates to be sure that they are all correctly formatted
Check the encoding of the files. If it is UTF-8, try to disable the BOM in both files.
Maybe it is a problem with your directory setting. Try to hard code the absolute path to check that.
These are the problems I can imagine, but I can't guarantee that it will work.
The answer Daniel Roseman gave is spot on, but there is a quick and easy way around this if pointing to your child template is not practical (as it might not be with more complex projects).
In your child template, remove the {% extends "" %} tags you have that are pointing to your parent.
In your parent template, replace {% block content %} with {% include "path/to/child/template" %}
That's it! Your child template will now load into the block content exactly as if you had rendered it directly.
There are a lot of problems. The short answer is "No, you can't change template dirs on-the-fly, and even if you could, you would do it definitely not the way you're doing it now"
Your main issue is that you're forgetting a comma in the TEMPLATE_DIRS setting. Try this:
TEMPLATE_DIRS = ( my_dir() + '/app/templates', )
Please disregard cheshire's answer.
Use os.path.join to combine 2 directories.
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
Here I am assuming that templates is a directory where you keep your templates. Now, to access the templates this is the base directory for them. So to extend base.html in some other file do like this -
{% extends "base.html" %}
...
{% endblock %}
Are you sure you have the proper template loaders setup?
You should have this in your settings.py:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
Related
Problem:
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block pagetitle %}
becomes
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block
pagetitle %}
Note that the {% tag %} is being broken with a new line. This causes syntax errors with django templates.
I've tried most top django template extensions and this does not fix the issue.
I've also tried these settings:
"[html]": {
"editor.formatOnSave": false,
},
"html.format.wrapLineLength": 0,
"html.format.enable": false,
"prettier.disableLanguages": ["html"]
Desired Behavior:
Automatically format *.html files, while preserving django template tags, not breaking them up with newlines.
Sub-optimal (but acceptable) behavior: don't format *.html files at all.
I had the same issue and the only way I found that solved it is to disable the default HTML formatter. Unfortunately, I did not find a way to make it format Django template tags correctly. You can do the same if you go to VS Code Preferences > Settings > User > Extensions > HTML and uncheck 'Enable/disable default HTML formatter'.
I solved this by following this advice: https://stackoverflow.com/a/73892745/1257347
TLDR: install the djLint extension (and remember to do $ pip install djlint)
I got it to work by simply adding {{""}} between the {% tag %} that were being broken.
Example:
{% extends 'main/base.html' %} {% block title_block %}Homepage{% endblock%}
{{""}} {%block style_ref_block%}{%endblock%} {{""}} {% block body_block %}
This Didn't work for me.
The hack I found was to set the vscode language to jinja instead of the auto detected html
reference
I've also just experienced vs-code misbehaving on django template tags (i.e. deleting curly braces).
I don't like the idea of disabling HTML formatting just to support templates (i.e. vs-code Preferences/Settings/Extensions/HTML: disable (uncheck) "HTML>Format:Enable"). This is arguably a step backwards, but it does stop vs-code misbehaving.
Instead, I chose to install (vs-code Preferences/Extensions) the 'Django' extension, by Baptiste Darthenay. This was a better way to go, because it works, gracefully, preserves native vs-code HTML formatting, and includes a nice set of django snippits, which saves me keystrokes when embedding template code. Tada!
BTW, before finding Baptiste's awesome extension, I also tried keeping vs-code HTML formatting enabled, AND enabling 'HTML>Format:Templating', which promised to "Honor django and other templating language tags"; it did not.
In my base.html template I have a link to a website that I would like displayed at the top of each page that extends it:
base.html
Results
Except for results.html, when I load that page I would like the link loaded as:
results.html
Home
I'm under the impression that the template language could solve my problem with an if statement:
if currentTemplate/urlRoute != results.html:
button = Results
else:
button = Home
Please help point me in the right direction to implement this if possible :)
Thanks :)
You can get current URL in template by using request.path. The request variable automatically gets passed into each template context if you use Django's RequestContext as recommended (you probably do, since it's the default way).
Then you can just do {% if "reports" in request.path %}......{% endif %}.
That said, a cleaner approach would be to put the link in your base.html in a {% block %} template tag, like this:
{% block top_link %}Results{% endblock %}
Then this URL will be the same in all the pages, and you will be able to override it in your reports page by just specifying another content for the block.
I have been doing lots of Googling on this topic and have gone through plenty of Django docs, but I can't seem to find a decent answer to what seems like ought to be a very simple question with a simple solution. So hopefully a Django vet out there can help.
Lets say I have the following urlconf:
urlpatterns = patterns('',
....
url(r'^directory/users/$', UserView.as_view(), name='users'),
url(r'^directory/users/(?P<user_id>[0-9]+)/$',UserView.as_view(), name='users'),
....
)
What I want to be able to do is test from within a template which pattern was followed, something like this:
{% if name_of_last_followed_url_pattern == 'users' %}
....
{% endif %}
Now one would think that Django would stash this away somewhere and be able to spit it back out to me. But I cannot find anything that corresponds to "name_of_last_followed_url_pattern" anywhere in the docs or in my searches. Any ideas how I might access this (or if not provided, why not)?
You need the context processors that adds the request to the template django.core.context_processors.request added to the TEMPLATE_CONTEXT_PROCESSORS. Then in the template you can do
{% if request.resolver_match.url_name == 'users' %}
....
{% endif %}
resolver_match has other attributes like namespaces, app_name. You can see here:
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/#django.core.urlresolvers.ResolverMatch
Problem
I'm using Django 1.3. I will have to use many different JavaScript functions (like 10 or something) in my template.
What I first did was to put them in the <script> tag, which worked fine. But now that it works, I want to separate them from the template code. It would make the code way more read'able.
So I thought of using the {% ssi "..." parsed %} thing. Since I use Django template tags in my JavaScript code, I can't just link them from my static files with <script src="..."></script>.
Here is what works :
# This will allow the {% ssi %} tag to include files from the given paths
ALLOWED_INCLUDE_ROOTS = (
'/THIS/IS/THE/FULL/PATH/TO/MY/PROJECT/static/js/',
)
Here is what does not work :
# Project root for further paths
PROJECT_PATH = os.path.dirname(__file__)
# This will allow the {% ssi %} tag to include files from the given paths
ALLOWED_INCLUDE_ROOTS = (
PROJECT_PATH+'/static/js/',
)
I double-checked that the two strings were the same (with ./manage shell) and they are exactly the same (with trailing / and all).
Questions
Why does the second code renders me [Didn't have permission to include file] in my template ?
Also, how should I link the file to include in the {% ssi %} tag ? Since {% get_static_prefix %} does not work, I'm currently using the file's full path, which is ugly.
As odd as it may appear, I didn't make any change in my settings.py but it is now functionnal. I believe Mike Cooper was right and some remote code was breaking ALLOWED_INCLUDE_ROOTS path.
ALLOWED_INCLUDE_ROOTS is likely a constant due to it's naming convention. Constants aren't meant to be variable.
http://en.wikipedia.org/wiki/Constant_(programming)#Naming_conventions
I'm trying to build my own template tags.
I have no idea why I getting these errors. I'm following the Django doc's.
This is my app's file structure:
pollquiz/
__init__.py
show_pollquiz.html
showpollquiz.py
This is showpollquiz.py:
from django import template
from pollquiz.models import PollQuiz, Choice
register = template.Library()
#register.inclusion_tag('show_pollquiz.html')
def show_poll():
poll = Choice.objects.all()
return { 'poll' : poll }
html file:
<ul>
{% for poll in poll
<li>{{ poll.pollquiz }}</li>
{% endfor
</ul>
in my base.html file im am including like this
{% load showpollquiz %}
and
{% poll_quiz %}
Bu then I get the the error:
Exception Value: Caught an exception while rendering: show_pollquiz.html
I have no idea why this happens. Any ideas? Please keep in mind I'm still new at Django
Shouldn't all custom filters be inside the templatetags directory?
templatetags/
__init__.py
showpollquiz.py
then
#register.inclusion_tag('show_pollquiz.html')
looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template
You forgot to close your template tags... Also, you should change the name in the for tag, you can't have for poll in poll:
<ul>
{% for p in poll %} <!--here-->
<li>{{ p.pollquiz }}</li>
{% endfor %} <!--and here-->
</ul>
Also notice you're not using the inclusion tag you defined at all. I think you got some code mixed up, try to go over a tutorial start to end and things will be clearer.
I'd not bother with writing your own template tags: take things one step at a time and stick to the basics for now. There's nothing wrong with {% include 'show_pollquiz.html' %}
I found the problem. The problem was that the #register.inclusion_tag('show_pollquiz.html')
inclusion tag is obviously looking for the template in the default_template directory. So that is why i got the error.
According to me this is not clear in the documentation. But I guess its how it is, being a template and all...
Oh , well.
Now, how would I put the #register.inclusion_tag('show_pollquiz.html') to look in the same folder as the app? under templatetags/?