I have components for the website and they all work fine, for example I have a navbar.html and works flawlessly. However, I think the issue begins when I have a static file. I have another component called 'header.html' (which contains a profile picture) and SHOULD have loaded up a header in my index.html file. But it spits 'TemplateSyntaxError' and says that the issue is with the line code '{% static ... %}'.
My settings.py seems correct (see below). I also included 'django.contrib.staticfiles' and {% load static %} in the main index.html.
This is also the html part:
The structure of my project is as such:
-myproject
--base
--myproject
--static
--staticfiles
Did you load static on the template?
Add {% load static %} to the top of your html file
Related
This question already has answers here:
How to serve static files in Flask
(24 answers)
Closed 1 year ago.
I am trying to display an image that is not in static folder but inside a logos folder.
The folder where my app.py is situated is this
app.py
static
logos <--- I want to access image from this folder.
{% for company in companies %}
{% if company.logo %}
<img src="/logos/{{company.logo}}" width="200" height="85"/>
{% endif %}
{% endfor%}
In HTML inspect element it comes properly as
<img src="/logos/59a94931df425f3034d2.jpg" width="200" height="85">
The file actually exists but the image is not displayed in webpage
The logos directory must be inside the static directory, in order to allow python to find the image and return that to the browser. In fact, if you check the console, you will see error 404: Not Found
So, put logos directory inside static and replace the url with:
src="logos/logos/{{company.logo}}.jpg"
Another way could be using url_for function (always inside your html file):
{{url_for('static', filename = f'logos/{company.logo}')}}
Let me know if it works.
Essentially what barbax has said, but your images to be rendered need to be within a static folder, and give this as the root directory when providing the path in html, e.g if this is your folder structure:
<ROOT>
└── app
└── static
└── logos
This should be your src value:
src='static/logos/{{ company.logo }}'
I've been looking at a few guides on applying bootsrap to django, and I am as far as having a static file in the root of my site, containing the css and js files within bootstrap, but when it comes to linking the files I am having an issue.
So I am trying to link the stylesheet inside of my base.html file but am not sure how to format it as for some reason using {% %} inside of the href field is being interpreted as part of the directory. In one of the videos I have watched he links it like thisBut I'm not really sure how to interpret this as trying to apply it to my own like so
href = "{% static 'artForAll/css/bootstrap.css' %}"
With the file structure for reference:
\AFA\src\artForAll\static\artForAll\css
I am given an error about a non existent file as it is taking the {%%} as part of the directory
My settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"static"),
]
File structure(blue highlighted file is where BASE_DIR leads:
I have a website with various js files, and i'm using django-compressor like so:
{% compress js %}
{% endcompress %}
{% compress js %}
{% endcompress %}
For some reason its only creating the FIRST js file. I have my app hosted on an EC2. I can see the first file there. The second file appears when I do a view source on the page, however, the file itself doens't exist (I get a 404 when I click on the link to the second js file). Is there anything I'm doing wrong? I have my settings as per below:
COMPRESS_ENABLED = not DEBUG
COMPRESS_PARSER = 'compressor.parser.BeautifulSoupParser'
COMPRESS_CSS_FILTERS = ['compressor.filters.cssmin.CSSMinFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']
It's working perfectly for css files...but failing on any other js file after the first compress flag...
I honestly have no idea what happened. this used to work perfectly, but randomly stopped out of no where! I did the following and it worked:
python manage.py compress --force
This basically forces it to compress all files; normally django-compressor ignores files that have not changed and have been compressed already.
I am really confused as to how could I load a custom tag filter from another app.
I have a similar problem like this Load custom template tag from another application?
And, I am doing it the same way, but still it doesnt load up and I am getting this error :
TemplateSyntaxError at /
'fillme_tag' is not a valid tag library: Template library fillme_tag not found, tried django.templatetags.fillme_tag,django.contrib.staticfiles.templatetags.fillme_tag,fillme.templatetags.fillme_tag
I have the app in settings installed app too.
I have tried loading it using various ways as mentioned below:
{% load fillme_tag %}
{% load fillme.fillme_tag %} #filleme is appname.
The structure is as follows:
my_project:
app1:
templates:
index.html (this is where i want to load custom tag)
views.py
__init__.py
fillme:
templatetags:
__init__.py
fillme_tag.py (the tag lib)
__init__.py
----- contents of fillme_tag.py ----
from django import template
register = template.Library()
#register.filter(name='demotag')
def demotag(value):
return value
It seems you missed fillme/__init__.py. Add it and this must work:
{% load fillme_tag %}
UPDATE
As error message said it couldn't open fillme_tag as it was invalid Library. My guess is you have a typo somewhere.
I'm using Django 1.2.4 on Ubuntu 10.10. I'm trying to override the index.html template for the admin module. I've been following these instructions. I also looked at this question, but I'm still having difficulty.
The instructions say to create an admin directory in the templates directory:
templates/
admin/
index.html
I want to override a single block in the index.html. (Really, all I want to do is append some text to the end. Is there an easier way than copy/pasting the entire block and changing it?) (Update: Looks like {{block.super}} may help.)
To signal that I'm overriding, I put this at the top of my index.html:
{% extends "admin/index.html" %}
Of course, that results in a stack overflow (from the terminal):
Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored
What is the correct way to do this? I tried a symlink per an answer on the linked question, but that resulted in the following:
me#mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me:
ln: creating symbolic link `django_admin': Protocol error
What am I doing wrong?
The recursion error is because you're extending the admin/index.html with itself.
You can either:
copy the entire admin/index.html template in your templates/admin/ directory, and it will replace the default template with yours
override the index.html per app or model, as explained here
I know this is late after the question, but you know, google traveling...
Amend settings.py with an extra template folder, for example:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/mysite/webapps/django/myproject/templates",
"/home/mysite/webapps/django/lib/python2.7/django/", # extra folder
)
Then in myproject/templates/admin add your own index.html like:
{% extends "contrib/admin/templates/admin/index.html" %}
{% block branding %}
<h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}
Variations are possible, obviously. This works on Django 1.3.1 final
Not sure if you found the answer, but you need to change
{% extends "admin/index.html" %}
to
{% extends "admin/base_site.html" %}
as that is what the original index.html page overwrites. Because the Django system searches your templates folder before using the default admin one, so in this case it finds the admin/index.html in your templates, then it's trying to extend itself with the extend (hence the recursion error).
For reference you can customise the base_site.html in you templates too, it extends base.html. The best thing to do is copy the original from:
/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/
and paste it into your templates folder as a starting point
I use an extra package, called django-smart-extends