Django load specific javascript file - django

I would like to load a specific javascript file for the specific html file. Instead of loading them into the base.html. Then a lot of pages will have unnecessary scripts that are not used.
I get this error:
Invalid block tag on line 3: 'static', expected 'endblock'. Did you forget to register or load this tag?
when i try to load it into the child.html. I'm
{% extends 'base.html' %}
{% block js %}
<script src="{% static "javascript/week.js" %}" type="text/javascript"></script>
{% endblock %}

You need to do {% load static %} in any template file that uses the {% static %} tag.

Related

How to add a logo to the site index bar using django

Can anyone help me on how to add a logo to the index bar in Django-admin site?
You should probably override (by extend) the app_index.html admin template.
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model
So you need to create a template/admin directory in your application and override the correct template by adding you own logo.
[Edit]
Oh sorry, you screenshot was too small and I just realized that you’re talking about the icon in the tabbar. That would be the favicon. For that, it’s simple, just add a favicon.ico into your static/ folder, and serve that directly from your http server (probably with a alias).
You can also serve the file directly using a simple view, but that doesn’t seems like a good idea except in development.
Your Question is answered here.
You have to override Django base.html template and put it under the admin directory.
https://stackoverflow.com/a/44428576/7122788
Override the Django base.html template and put it under the admin directory like my_app/templates/admin/base.html
Add {% block extrahead %} to the overriding template.
{% extends 'admin/base.html' %}
{% load staticfiles %}
{% block javascripts %}
{{ block.super }}
<script type="text/javascript" src="{% static 'app/js/action.js' %}"></script>
{% endblock %}
{% block extrahead %}
<link rel="shortcut icon" href="{% static 'favicon.ico' %}" />
{% endblock %}
{% block stylesheets %}
{{ block.super }}
{% endblock %}

Django conditional javascript file in template causes missing staticfile error in production only

I have a conditional in a template used by several views which will include a js file if passed in by the view:
in the template:
{% if js_file %}
{% block inner_js %}
<script defer type="text/javascript" src="{% static js_file %}"></script>
{% endblock inner_js %}
{% endif %}
which is eg used by a view by:
.......
context = {'title': 'Sign up',
'form': form,
'js_file': 'js/supplier.js'}
return render(request, 'pages/signup.html', context)
This produces no errors in development, but when pushed to production I get the error:
ValueError at /users/example/
Missing staticfiles manifest entry for ''
If I remove the template if block above then this error dissapears (and works fine for those views which use the template without a js file).
I would rather not have to create a new version of the template for every view which uses a js file, is their a better way to fix this? (seems a bit of a weird error).
I don't believe you can conditionally include/exclude a block as you're doing. If you put the if tags inside the block, it will only include the <script> tag when the variable js_file is populated.
If you're conditionally including this block to override another block called inner_js in a template higher up, you can do something like this to achieve the same results:
{% block inner_js %}
{% if js_file %}
<script ...></script>
{% else %}
{{ block.super }}
{% endif %}
{% endblock inner_js %}
{{ block.super }} is the equivalent of calling Python's super in a class, and allows for block extensions in templates that extend a base.

Custom tag not loaded in template

I've created a custom tag that I want to use, but Django can't seem to find it. My templatetags directory is set up like this:
pygmentize.py
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from django import template
from pygments.formatters.other import NullFormatter
register = template.Library()
#register.tag(name='code')
def do_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
return CodeNode(code,nodelist)
class CodeNode(template.Node):
def __init__(self,lang,code):
self.lang = lang
self.nodelist = code
def render(self,context):
code = self.nodelist.render(context)
lexer = get_lexer_by_name('python')
return highlight(code,lexer,NullFormatter())
I am trying to use this tag to render code in gameprofile.html.
gameprofile.html
(% load pygmentize %}
{% block content %}
<title>{% block title %} | {{ game.title }}{% endblock %}</title>
<div id="gamecodecontainer">
{% code %}
{{game.code}}
{% endcode %}
</div>
{% endblock content %}
When I navigate to gameprofile.html, I get an error:
Invalid block tag on line 23: 'code', expected 'endblock'. Did you forget to register or load this tag?
For Django 2.2 up to 3, you have to load staticfiles in html template first before use static keyword
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
For other versions use static
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
Also you have to check that you defined STATIC_URL in setting.py
At last, make sure the static files exist in the defined folder
The error is in this line: (% load pygmentize %}, an invalid tag.
Change it to {% load pygmentize %}
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
in your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.
did you try this
{% load games_tags %}
at the top instead of pygmentize?
{% load static %}
Please add this template tag on top of the HTML or base HTML file
Encountered this same issue but I just added {% load static %} from my extends template and it worked. So in your case if you're trying to load gameprofile.html from a base template you just need to add it like this:
{% extends 'some_base.html' %}
{% block content %}
{% load pygmentize %}
I had the same problem, here's how I solved it. Following the first section of this very excellent Django tutorial, I did the following:
Create a new Django app by executing: python manage.py startapp new_app
Edit the settings.py file, adding the following to the list of INSTALLED_APPS: 'new_app',
Add a new module to the new_app package named new_app_tags.
In a Django HTML template, add the following to the top of the file, but after {% extends 'base_template_name.html' %}: {% load new_app_tags %}
In the new_app_tags module file, create a custom template tag (see below).
In the same Django HTML template, from step 4 above, use your shiney new custom tag like so: {% multiply_by_two | "5.0" %}
Celebrate!
Example from step 5 above:
from django import template
register = template.Library()
#register.simple_tag
def multiply_by_two(value):
return float(value) * 2.0
The app that contains the custom tags must be in INSTALLED_APPS. So Are you sure that your directory is in INSTALLED_APPS ?
From the documentation:
The app that contains the custom tags must be in INSTALLED_APPS in order for the {% load %} tag to work. This is a security feature: It allows you to host Python code for many template libraries on a single host machine without enabling access to all of them for every Django installation.
In gameprofile.html please change the tag {% endblock content %} to {% endblock %} then it works otherwise django will not load the endblock and give error.
You need to change:
{% endblock content %}
to
{% endblock %}

django compressor is it possible to compress all files to one file?

Is it possible with django compressor?
Or, can I name a {% compress %} tag and later add some additional files there? (to be compressed together)
For example, assume in layout.html I have:
{% compress js file main_js_files %}
<script src="..../a.js"></script>
{% endcompress %}
and then in template.html: (which extends layout.html)
{% compress js file main_js_files %}
<script src="..../b.js"></script>
{% endcompress %}
and finally there will be one single file, contains both a.js and b.js.

Django: nested template commands

I am following the tutorial of Tango with Django. When I came to this part of writing nested block template commands in about.html:
{% extends 'rango/base.html' %}
{% block title %}About{% endblock %}
{% block bodyBlock %}
<img src="{% static '/rango/images/pythonDjango.jpg' %}">
{% endblock %}
The system complains with the error message: Invalid block tag: 'static', expected 'endblock'
How should the above nested commands be arranged?
The static tag is not loaded into the template. So, at the top of the template, add:
{% load static %}
Everything else with the posted template looks fine.