Do I really need to use {% load static %} in Django Templates? - django

I am using Django 3, the latest version. I have defined the static files directories properly as required.
Currently to display an image, both the following source codes work fine.
Code 1:
<img src="static/img/logo.png">
Code 2:
{% load static %}
<img src="{% static 'img/logo.png' %}">
Since both the code snippets are working and running fine without any problems, I am wondering why not to simply use the way of Code 1 above and refrain from the extra lines of Code 2.
Which one would be beneficial and why? Please guide.

The base/master level templates would work without static tag and maybe considered for performance.
However for other level templates either nested or in app templates or nested urls, it's required to be reference with static as it would minimise chances of error. So for all other templates it is recommended to use static tag.

Related

Why is {% load static %} a dependency for {% get_media_prefix %}?

I've been using {% get_media_prefix %} for a very long time. I was explaining this to someone when he pointed this out.
Why do I need to declare {% load static %} in order to use it?
It even uses in the documentation's example code here.
To an extent I understand that static files and media files are similar in nature. Even when we use them with combination of nginx+gunicorn, nginx handles both of them(we let everything else proxy, but not these).
But still we have a separate MEDIA_URL and STATIC_URL as well as MEDIA_ROOT and STATIC_ROOT defined for these files.
Then why {% load static %} needs to be declared in order to use {% get_media_prefix %} ?
Thanks in advance.
In order to use a template tag in your HTML you must first load the module that contains it.
So, accorrding to the source code of get_media_prefix template tag, this template tag lives inside django/templatetags/static.py.
That's why you have to load it every time you use it, in every HTML template.
This, of course, applies to every template tag usage. At the top of every HTML file you load the template tags and then you use them. Just like you import a function in your python code.
UPDATE: From the Django 1.3 release notes:
In previous versions of Django, it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL. Part of the purpose of introducing the staticfiles app is to make it easier to keep static files separate from user-uploaded files. Static assets should now go in static/ subdirectories of your apps or in other static assets directories listed in STATICFILES_DIRS, and will be served at STATIC_URL.
As you can see, Django used to treat both static and media the same. Since Django 1.3 this changed, but the template tag not. No big deal. It's just a convention.

{% load staticfiles %} - Does it pre-load all recursively or on demand?

In django I'm curious concerning the {% load staticfiles %} template directive.
In static files I have a sass directory, and sass in turn can have a cache which gets quite large. I'm not inquiring of 'best practice' though, but in any case are all files pre-loaded or not?
If I'm only using, for instance, fonts, bootstrap, a personal stylesheet, etc...is it going to load all the things I'm not using?
Very curious about this. I don't want to use up more resources than needed.
You've completely misunderstood what the load tag does. All it does is make a template tag library available for the template to use: in this case, the "staticfiles" library which includes the definition of the {% static %} tag. Without that load statement, you can't use that tag.
It doesn't do anything with the staticfiles itself - indeed it can't, loading them is a matter for the browser, which will do whatever is in your HTML.

Parsing static stylesheets into template

I have 6 stylesheets which i am generating with LESS. Since i want to automate this procedure i would like to know if there is any way to parse the stylesheets directly into my template? I need a hybrid between the include and static tag. Static only gives a path, while i want the include functionality being able to parse it.
Is it possible to combine them include and static somehow:
{% include {% static 'test/test.js' %} %}
Update: Its very important i mention that the test.js also contains {% static %} tags.
If it needs to be parsed as a template, it's not a static file and shouldn't be stored in the static directory. That's for assets that need to be served directly from your webserver.
Put it in your templates folder - in a "scripts" subdirectory if you like - and use the standard include tag.

Referencing images from template

I want to insert an image into my template. The image resides in the same folder as the template.
I tried:
<img src = "imageName.png" />
but for some reason, this wouldn't work.
Does anyone have an idea why that is?
Don't put images in the same folder as templates. Images are a part of static content. You should read about managing static files in django.
Hope that helps.
I'm guessing you're new to the whole django scene, so don't be afraid to hit a few boulders on the way. In fact, this is a problem that I faced too, since I just usually put everything on a web-server, with an index.php file (yes, the horror), and everything was just relative.
So, let me give you a little context, what you are trying to embed on a page is called a static file. What that basically means is a file that does not change, a file that is not dynamic. Now, since these static files require no processing, they are treated specially, and are put inside a static folder. You can see where your static folder is when you check your main settings.py file.
Now often, people make a lot of mistakes with static files, because there are so many variables that have a STATIC infront of them. I know, its totally counter-intuitive, but there are reasons why they're there. So, let me direct you to something that can be a little help in understanding this whole fiasco.
When using static files, you usually use a few special tags. You can learn more about them here. But, I'll just show you how you would embed your image into your html, just as a sample.
{% load staticfiles %}
<img src="{% static "images/myphoto.png" %}"/>
So, how should your directory look like? Well, what you would have is something like this
STATIC_FILES_FOLDER > IMAGES > myphoto.png
Hope that helps.

How to run JSLint / JSHint in a file with templates?

I'm trying to introduce some tools at work to improve code quality. An obvious solution that I'd used at a previous company was to run jslint before checking in code.
The problem is that we are using Django to do our templating (though, I assume we would have a similar problem with other templating languages).
How is it possible to take code like the below, and have it JSLint/JSHint properly while ignoring the template tags?
var a = { "test" : "test"};
{% comment %}
{% endcomment %}
{{ my_variable }}
window.x = "y";
I've seen this question specifically regarding JSHint, which looks like it could handle some cases, but it doesn't address inlining variables, like {{ my_variable }} above.
Is there any way to ignore certain lines using JSHint/JSLint, or otherwise get the linting to execute correctly?
Much like linting coffeescript with these tools, you're trying to lint content outside it's pervue. Probably easiest to run the template with dummy values, and JSHint that resulting file.
Probably duplicate of How to run JSHint on files with Django template markup in them?