Given the path to a Django template, is there any easy way to find a list of the template's inheritance chain? For example, if mytemplate.html inherited from user-section-base.html, which inherited from global-base.html, it would be something like:
>>> print(get_template_chain('myapp/mytemplate.html'))
>>> ['/project/myapp/templates/myapp/mytemplate.html', '/project/myapp/templates/myapp/user-section-base.html', '/project/myapp/templates/myapp/global-base.html']
I'm trying to write a static site generator that automatically regenerates static pages from Django templates, and I need to detect when the underlying template changes since last generation. For a single flat template with no inheritance, this is trivial, but for a template that inherits from a parent, which also inherits from a parent, I need to track changes of those files as well.
However, I can't find any simple way to retrieve this, short of manually parsing the {% extends ... %} tags directly. What's the best way to do this?
the cleanest way is to hook to the template rendering engine, and try doing it there. That however, will require a deep dive into its code. Unless you are already familiar with the code base, I would say that going for parsing is the easiest and fastest option.
This is a very hacky way ;-) ...but if you are open to changing Django code, you can go to django/template/loader_tags.py and change the ExtendsNode.render return statement from
return compiled_parent._render(context)
to
return compiled_parent._render(context) + f'\n<!-- {compiled_parent.origin} -->'
and the rendered template will end with the full path of all your template-extends, e.g. for the file I'm working on right now:
</body>
</html>
<!-- c:\srv\lib\admindash\admindash\templates\admindash\html5.html -->
<!-- c:\srv\lib\admindash\admindash\templates\admindash\bootstrap3.html -->
<!-- c:\srv\lib\admindash\admindash\templates\admindash\site-admin.html -->
<!-- c:\srv\www\school\templates\site-admin.html -->
<!-- c:\srv\www\school\templates\page-admin.html -->
<!-- c:\srv\lib\dashboard\dashboard\templates\dashboard\tctr\home.html -->
<!-- c:\srv\lib\studentdb\studentdb\templates\studentdb\tctr\tctr-base.html -->
<!-- c:\srv\lib\studentdb\studentdb\templates\studentdb\tctr\candidates\candidate-base.html -->
Hooking into the template engine would be nice, but I'm not sure it can work out-of-the-box since .render(..) returns a string and thus loses all meta-data. Perhaps if you create your own {% extends ... %} tag..?
Related
I have a template (using jinja2) that is used in lots of views (via {% include %} statements). The template will always require (amongst other data that changes, and will be passed in via the context) a dict, that is a constant. It would be cumbersome to add the dict to the context in every view that will potentially use the template.
I could add the dict as a global variable in the jinja2 env.globals, but that doesn’t seem like a particularly good idea either. It is only needed in one template in one app, not the entire project.
The Jinja2 documentation mentions template globals specific to a particular template, but I can’t figure out how to implement this with Django.
Any help appreciated, thanks :)
EDIT: Some additional context.
The template provides some information to be used in lots of other html documents (via the include tag). The positions of the lines and polygons are the same, but the colours are different. I don’t particularly want to hard-code all of the x1=”20” x2=”30” etc, so it would be useful to have all of this stored in a dict, and do something like the following:
{# Template to be included #}
<svg class="a" width="200px" height="200px" viewBox="0 0 100 100" preserveAspectRatio="none">
{% for i in range(10) %}
<line {{dict_always_available[i]}} class="{{info_passed_in_context[i]}}"></line>
{% endfor %}
</svg>
I don't want to have to pass dict_always_available into the context in every view that will potentially (but not necessarily) need it.
So, I've been editing a website and have many JavaScript functions that utilize the Contexts that the views.py file passes to the page. Until now, these functions have been contained in the base.html file and so have been loaded onto every page directly. However, to make things cleaner, I copy and pasted all the functions to an external .js file. Now, rather than use the contexts, the functions consider them to be literal strings.
Example:
$('#title').text('{{ event.name }}');
The above line will actually set the text of the element to say "{{ event.name }}" rather than the name of the event. Any ideas on how to fix this? I really don't want to keep these functions in the base file where they can be seen by anyone who inspects the page source.
It doesn't matter if you put your javascript functions in an external file or in your base.html it would still get exposed to the user. Only a minification of the file would actually help to trick the user from seeing the actual values but with javascript all your code is public.
Why you're having this problem is because when you rendered the javascript inline (in your base.html) you had access to the template context.
This is no longer the case and the Django template engine doesn't interpolate your {{ event.name }} anymore.
The problem you're facing as well is a good one. You should never mix and match javascript with Djangos template language or any template language for that matter and the only way of fixing it is to
a) start pulling the values from the DOM ie. render a proper DOM
b) to start to fetch the values from the server, traditionally using AJAX.
And the smallest example that I can muster at the moment is below:
Your view:
def my_django_view(request):
return HttpResponse(json.dumps({'meaningoflife':42}), mimetype='application/json')
Your HTML
<input type="hidden" id="myMeaning" value="{{ meaningoflife }}" />
Your javascript
var meaning = document.querySelector('#myMeaning').value;
alert(meaning); //should alert 42.
In your view you return some form of render_to_response which takes a template argument and a context argument. What the render_to_response function does is read your template, and replace all {{ placeholders }} with the values passed via the context dictionary.
Templates are essentially a complex version of this
"""
<h1>{{ person.name }}</h1>
<p>{{ person.phone_number }}</p>
""".format(person)
The problem is the templating engine does not know files specified by a scripts src attribute is actually a Django template. To fix this don't use the script src attribute. Instead do something like this.
<!--base.html-->
<h1>Site Title</h1>
<p>Some content</p>
<script>
{% include 'jsfile.js' %}
</script>
Using the include statement should do the trick.
If anyone here is ASP.NET pro, you might know what I mean by user control. I wish to create a similar one in django instead.
So, my problem is that I have several pages in my website, but I need a search bar to appear in every pages. Since I require the views.py to operate this search bar, I cannot do a simple method of
{% include 'something.html' %}
Therefore, can anyone suggest how can I do it?
There are a couple of ways to accomplish what you're wanting to do:
Context Processors
Template Tags
Context Processors can augment the template context with values, regardless of which template is loaded. They are akin to filters in Rails.
Template Tags, like Context Processors, can accomplish anything you can do in Python, but are implemented at the template level.
If you need something to be present on every template, one of the simplest ways to accomplish this is with an inclusion tag, which can also accept values passed to it. An inclusion tag could be implemented at your highest level template, a.k.a your MasterPage, and as long as you don't put it in a block and override it, it would appear on every page that includes that template in its inheritance chain.
If it's just something you want to include on every page, and it doesn't need to do any processing, you should just be able to place the code you want in the top-most template and have subsequent templates inherit that.
I typically have a "base.html" template that all of my templates inherit from. If I need something to be in every page, I put it there. If it's something I want there by default, but want to be able to augment it in subsequent templates, I will place it into a block. That block will let you include or override its default content.
I know this post is kind of old but I just came across it and found a kind-of-solution that works. I call it kind-of-solution because it is a workaround.
I have a few different sites on which I want to display logging information. This display always looks the same (it has the same html) and has the same database table and model class behind it.
My solution/workaround uses the django filters:
in views.py I put the list of log-entries in the context
context = {'list_log': Log.objects.filter(condition = True) }
template = loader.get_template('my_html_file.html')
return HttpResponse(template.render(context, request))
in my_html_file.html I use a custom filter
{{ list_log|get_log_uc|safe }}
in the filters.py I load another html file with this custom filter
#register.filter
def get_log_uc(list_log):
template = loader.get_template('user_control_log.html')
context = { 'list_log' : log }
return template.render(context)
in user_control_log.html I have the user control equivalent html
{% for log in list_log %}
<p>log.something</p>
{% endfor %
I have a pretty straight forward question, in regards to joomla templates.
The end result being : http://css3playground.com/flip-card.php
What I want to do is simple, in a sense, but need to know where to look;
I want to have the entire page wrapped in two divs, all the PHP code, to which class i can define in css and drop in some javascrpt so I can apply page transitions to that div. All of which I know how to do except for where to do it in, the PHP structure of joomla is new to me.
and also, after the first step is accomplished, create a second div after the content that would be dynamically loaded with content from clicked links on the page from within the template, but thats two questions at once lol.
Any ideas on the first part?
If you just want to use a div to encompass the entire template, do exactly that: wrap the template in a div and give it a custom class or id:
<html>
<head>
//stuff here
</head>
<body>
//insert the wrapper here
<div id="wrapper">
//template structure here
</div>
</body>
</html>
The file you want to edit will likely be named index.php located at public_html/templates/your_template/index.php.
For some templates, such as those by Yootheme, you will instead want to edit the file at public_html/templates/your_template/layouts/template.php (or /public_html/templates/your_template/styles/current_profile/layouts/template.php if you're using a profile other than the default).
In a Satchmo Store, I need to attach a small .png (a barcode) to an email that django sends on completion of the order. The email is formatted in HTML using send_order_confirmation() which calls send_store_mail() (both part of satchmo.) Neither of these functions offer the ability to attach a file (I think) so should I just re-write them? I was wondering if it is possible/better to do this using signals. Maybe rendering_store_mail() ?
By the way, the barcode would be dynamically generated, so there's no way of having a link to a file on a server somewhere.
Many thanks,
Thomas
well I too had to add extra infos to the confirmation emails, only text though. So this would be the very easy way to add extra-stuff to emails using signals, which IMHO, is the best approach to do it. Always use signals if you can avoid overriding the satchmo-core ;-)
define your listener to add some context for the rendering. In this case I'm adding the contents of an extra notes field, and the barcode for this order, supposing there is a function named get_barcode_img(<order>), to the context. I'm supposing here too, that the get_barcode_img function would return not just a png, but something like a MIMEImage (like from email.MIMEImage import MIMEImage) to be able to just include it inline. Also, there might be more infos needed, like a MIME-header for the img.
# localsite/payment/listeners.py
def add_extra_context_to_store_mail(sender,
send_mail_args={}, context={}, **kwargs):
if not 'order' in context:
return
context['barcode_header'] = get_barcode_header(context['order'])
context['barcode_img'] = get_barcode_img(context['order'])
context['notes'] = context['order'].notes
connect the listener to the signal somewhere where the code will be "discovered" for sure, like models.py:
# localsite/payment/models.py
from satchmo_store.shop.signals import rendering_store_mail, order_notice_sender
rendering_store_mail.connect(payment_listeners.add_extra_context_to_store_mail, sender=order_notice_sender)
override the templates locally (e.g. order_placed_notice.html) to add the new context. Be aware where you put your templates, as the path is essential for django to take your new template instead of the satchmo's one. In this case, starting from your project's root-path, there could be a templates-folder and inside it, there must be exactly the same path as in the satchmo-folder. E.g. /templates/shop/email/order_placed_notice.html ... this can be applied for any "valid" templates-folder inside an app. It's up to you to decide, where/how the templates should be organized.
<!-- templates/shop/email/order_placed_notice.html -->
<!DOCTYPE ...><html>
<head>
<!-- include the image-header here somewhere??? -->
<title></title>
</head>
<body>
...
Your comments:
{{ notes }}
Barcode:
{{ barcode_img }}"