Custon DjangoTemplates for one app - django

I want to use a third party app that did not use adequate django principles:
templates \w hard coded urls instead of {% url ... %} syntax
templates \w hard coded static files instead of {% static ... %} syntax
makemigrations is left to the user/deployer
all templates directly in templates folder, like 500.html, login.html, etc
interesting nested app-structure, where some apps are loaded depending on the configuration (i.e. advanced settings.py logic manipulating INSTALLED_APPS)
These make it hard to include the app in urls /suburl/.... I spent 15 minutes thinking this was rewritable, and a git PR would be a good idea, after realizing it might not be.
The most crucial problem, hard coded urls, is in the templates. Hence I want to copy the templates to my parent-project in a subfolder, fix them with generated urls, and redefine template-resolution for this particular app, but not for all apps.
This should keep my template-namespace relatively clean. Also, this way I avoid having to change all the view-functions to specify a better namespaced template. I don't want to get too involved in these, because these functions are massive.
Can I do this, define a customized DjangoTemplates for ONE specific app, but not the others?

Related

(Django) How to include template file from app into base.html in templates

I am building a site in Django CMS. In my templates directory for the project is base.html.
I am writing an app "was_this_helpful" to add a dialog box on some pages for users to give feedback. I want to include a file from was_this_helpful/templates into base.html but it says the file does not exist.
{% include 'was_this_helpful/dialog.html' %}
My file structure look like this:
- was_this_helpful
- templates
- was_this_helpful
- dialog2.html
- dialog.html
- required app files
I read somewhere that sometimes template files need to be another level deeper in templates to be found which is why I made the dialog2.html but still it's not working. I do not understand how to accomplish this. Based on what I've read it should work. Is it different because I'm not in another app, just the templates directory?
Without knowing more it's hard to tell if it's a simple solution or not.
The way you have your code written, there is not a was_this_helpful/dialog.html - you only have a dialog2.html inside your was_this_helpful so was_this_helpful/dialog2.html would be the reference path.
I've always created another folder inside my templates folder with the name of the directory above my templates folder. Just like you have with your was_this_helpful second directory. I find that this makes it much easier to extend base.html files.
You can always do it absolutely too by two periods before the path call, so ../was_this_helpful/templates/dialog.html
If you don't have luck with that either, there is an {% extends %} method as well which might accomplish what you're trying to do as well.
Good luck!

What's the best way to DRY in Angular within Django?

I'm new to Angular, and to Django, and to DRY (Don't Repeat Yourself) so forgive me if this really simple and obvious... I've somehow missed it.
I have a dozen or so partials and a couple of them share a similar code block. I've learned how to use {% include 'template.html' %} to keep things DRY in django templates, however this doesn't work inside client side partials.
So I looked into ng-include with a <div ng-include="/groups/template.html"></div> and while that kind of works it changes the scope (which is over my head) so I cannot get my partials code to work on that included codeblock. But my biggest issue here is that the code block I'm including has to be via a routable URL in the urls.py file. This seems to go against security and usability.
Is what I'm asking for even possible? I'd love Angular to grab an external codeblock and drop it into the partials file server side when it puts the partials file into the cache.
Your Angular templates should be placed in your static folder like js files.
The structure can be like this one:
static/
js/
views/ #Put here your angular templates
Then, you can call ng-include like this:
<div ng-include="{{ STATIC_URL }}views/template.html"></div>

What are the pros and cons in serving CSS and JavaScript using Django template system

Often, I encounter scenarios where I see it makes sense to use template tags in CSS and JavaScript files, such as the use of {{ STATIC_URL }} in CSS to access image. I understand the only way to achieve this is to have CSS and JavaScript files served by Django. I am interested in this approach. But before I commit, I want to hear you experts' experience on it. What are the pros and cons of this approach? Thx.
Pros:
You can make a lot of per-request decisions about how things look and behave.
You can keep the number of different CSS/JS files to a minimum.
Cons:
Browsers tend to cache CSS and JS aggressively, so you'll need to use some aggressive anti-cache techniques. Of course, this means disabling caching for some/all static files.
Every CSS and JS request will consume another thread of your WSGI server. In a normal request/response cycle, each request generally takes up one thread; you're effectively tripling this, at least, so now your app that could handle 200 simultaneous requests now can only handle 66.
When your site makes it big, a CDN probably can't help you.
Alternatives:
Tweak the CSS via javascript, and set a javascript variable inside your page template to control the tweaks.
Use multiple CSS files and control their inclusion dynamically.
Generate static files as needed, but then cache them to disk and serve them via mod_xsendfile. This only works if you are serving static files from somewhere the django process can write to, such as on the same machine or a network mount.
Personally, I've been sticking with the Django team's advice to make CSS and JavaScript static files, served directly by the server instead of via Django. It hasn't been a problem and has simplified a lot of things. Generally, any time I think I need a dynamioc CSS or JS file, there's a way to refactor so I don't.
For example:
the use of {{ STATIC_URL }} in CSS to access image
I'm not sure how variable your {{ STATIC_URL }} is, but I've found that using the <base> tag in my pages fixes a lot of things. I assume this is for background images? Could you update your question to give an example?
Another thing I've done is, if my JavaScript needs dynamic data, I'll put most of the code in a JavaScript library I serve as a static file and then put the minimum dynamic stuff in a <script> tag at the end of the page. Usually I'll put it all in an object (looking a lot like JSON) and then just pass that object to a function. Come to think of it, you could just take all the dynamic stuff, make a dictionary out of it in your view function, encode it into JSON, and pass it via context. Then your page template just looks something like:
<html><head>
...
<script src="{{ STATIC_URL }}/js/foo.js"></script>
...
</head><body>
...
<script>
foo_main({{ foo_params_json|safe }});
</script>
</body></html>
This makes it a lot easier to reuse this code.

Django: include external include files

Im building a web app which will be part of an existing static website. I'd prefer to use the header and footer from the current site which are static .inc include files.
Is there a way to include these files something like:
{% include 'http://www.mysote.com/inc/footer.inc' %}
There isn't a built-in way to do this in Django, but it would be a really easy template tag to write on your own (there's a decent chance someone has already written such a thing, though a quick search didn't turn it up for me). If you want to go that route, you can do that with a quick simple_tag (documented here: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#shortcut-for-simple-tags). It could probably be as simple as something like:
def include_external(url):
import urllib2
return urllib2.urlopen(url).read()
register.simple_tag(include_external)
{% include_external 'http://....' %}
However, as Umang mentioned, that is potentially problematic--fetching that include file will probably significantly increase your page load time, and you'll guarantee that a failure in your static site will bring down your Django app as well. If either of those things turns out do be a concern, you could look at caching the header--however, that's adding additional complexity, and you might be better of just copying your header file over each time it's updated.

How to make project templates and Satchmo templates co-exist?

I'm working with a Satchmo installation that resides within an existing project. This project has its own templates as well as templates for some of the various apps that are installed. Some of these app-specific templates have their own app_base.html variations that expect to derive form base.html. I'd like to be able to do the same thing with my Satchmo templates and have them reside within my project's base, but also have some additional html added around all of them.
/templates
base.html
index.html
/news
news_base.html (extends base.html and adds news-specific template features)
index.html
detail.html
/store
base.html (overriding Satchmo's base)
This structure works somewhat, but not how I expected. in /store/base.html (Satchmo's base) I've simply replaced everything with a test message. I can see the message, so I know satchmo is loading its base and not the site's base. However, I can't extend my project's base anymore since using:
{% extends "base.html %}
Yields a recursion error since its calling itself and the following simply won't work.
{% extends "../base.html" %}
I realize that I can change my project's base.html to a slightly different name and point all app-specific templates at them, but it seems like a pretty major hack on such a fundamental aspect of the template structure.
Hmm, I didn't think django looked up templates relatively like that.
Kinda crazy hack, but this should work:
/templates/store/base.html extends "global_base.html"
/templates/global_base.html extends "base.html"
Depending on how you have your template structure set up, it might also be a good idea to play with the settings.TEMPLATE_LOADERS variable.
TEMPLATE_LOADERS Default:
('django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source')
A tuple of callables (as strings) that
know how to import templates from
various sources. See The Django
template language: For Python
programmers.
For more information on how this affects the template loading process:
http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
From the way you describe your problem, it seems like by commenting out the "app_directories.load_template_source" file line, you might be able to better find a way to accomplish what you're doing.
django.template.loaders.app_directories.load_template_source
Loads templates from Django apps on
the filesystem. For each app in
INSTALLED_APPS, the loader looks for a
templates subdirectory. If the
directory exists, Django looks for
templates in there.
This means you can store templates
with your individual apps. This also
makes it easy to distribute Django
apps with default templates.
For example, for this setting:
INSTALLED_APPS = ('myproject.polls',
'myproject.music') ...then
get_template('foo.html') will look for
templates in these directories, in
this order:
/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html
Note that the loader performs an
optimization when it is first
imported: It caches a list of which
INSTALLED_APPS packages have a
templates subdirectory.
This loader is enabled by default.
I just had this same problem. It looks like the satchmo developers planned for this by putting an "empty" base in the shop template directory. While this may not be relevant to you anymore, I would have like to have seen this here.
You can make a "shop" directory in your template directory and copy the main satchmo base.html to that directory.
This worked for me.