So I started working on this django project that I took over from someone else. There are several settings files that all inherit from a base.py settings file.
In this base.py settings file there are several static image files defined in there.
For Example:
LOGO_URL="/img/logo.png"
LOGO=STATIC_URL+LOGO_URL
This seems strange to define image locations in this way. Is this best practices? if not what is the best practice?
Thanks
More commonly you would see just the STATIC_URL defined in your settings, and then the logo would be accessed in a template with:
{% load staticfiles %}
<img src="{% static 'img/logo.png' %}" />
The docs give more detailed explanation.
Related
I'm not quite sure what's going on, but my images loaded fine on my local development server. However, once I deployed, the images will not load. I checked the URL and it seems to be correct. I'm not sure what's going on here.
base.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
HTML
{% load wagtailcore_tags wagtailimages_tags %}
...
{% with post.main_image as main_image %}
{% if main_image %}{% image main_image fill-400x200 %}{% endif %}
{% endwith %}
Page Source for Element
<img alt="photo" src="/media/images/Lily.2e16d0ba.fill-400x200.jpg" width="400" height="201">
I'm pretty confused and I can't really find much about the topic. Am I the only one who's run in to this issue? Any help would be greatly appreciated.
During development (when you use ./manage.py runserver and have the DEBUG setting set to True), Django serves static files itself as a convenience. In production, it's up to you to configure your web server to serve static files from /media and /static: https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/
This is for performance and security reasons - there's no point having static files served via Python code when there's already a web server properly tuned for that task.
I'm trying to use django's static templatetag to display an SVG, but it doesn't seem to recognize the SVG as a valid image url. This is what I currently have:
settings.py
import mimetypes
mimetypes.add_type("images/svg+xml", ".svg", True)
landing.html
{% load staticfiles %}
<img src="{% static 'images/right-arrow.svg' %}" />
At least in my view.py, it recognizes the SVG mimetype:
views.py
print(mimetypes.guess_type(static('images/right-arrow.svg')))
# returns ('images/svg+xml', None)
The SVG does display in a non-django page, and it will download the SVG if I try to open the SVG path in a new browser tab.
I'm currently using python 3.4 and django 1.8.4.
I found the issue. In settings.py, it should be mimetypes.add_type('image/svg+xml', '.svg', True). image should be singular.
I faced a similar issue.I would recommend you to use :
src="{{ STATIC_URL }} images/right-arrow.svg" instead of src="{% static 'images/right-arrow.svg' %}"
svg format might not always identify django's method of obtaining staticfile contents.Hope this helps :)
Add this in your settings.py file.
import mimetypes
mimetypes.add_type("image/svg+xml", ".svg", True)
mimetypes.add_type("image/svg+xml", ".svgz", True)
In your cases you have added images in add_type which should be singular (image).
You are loading staticfiles and using static?
This is wrong.
Try changing {% load staticfiles %} <img src="{% static 'images/right-arrow.svg' %}" /> to
{% load static %} <img src="{% static 'images/right-arrow.svg' %}" /> and you also need to consider which app you should find your static files.
I'm use flask-assets for bundling, minification and versioning (making sure that when we change a CSS or JS file, the browser loads the new version, instead of what's in its cache... but of course we want it to load from the cache subsequently).
Since the site only has a few pages and they all use different resources, I've defined the bundles in the templates themselves, as described under Templates Only in the docs:
{% assets filters="jsmin", output="gen/our-page_packed.js",
"blah.js", "yadda.js", "rhubarb.js", "wibble.js" %}
<script src="{{ ASSET_URL }}"></script>
{% endassets %}
{% assets filters="cssmin", output="gen/our-page_packed.css",
"css/foo.css", "css/bar.css" %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
ASSETS_DEBUG is False in production, and the site is not localised, nor served through a CDN or with S3.
The problem is, when we push to production, the bundles initially apparently aren't being created. The pages lack CSS and Javascript, and the apache error log contains errors like this:
File does not exist:
/srv/our-client/our-client/static/gen/our-page_packed.css, referer:
https://app.our-client.com/quux/123/xyz/
After we reload the page a number of times, over the course of a minute or two, it all starts working. We occasionally hear customer complaints about what sounds like missing CSS or JS, but it doesn't seem to persist and it's not clear it's a related issue.
I'm afraid I'm far from a Flask expert (the site was created by another developer; I added flask-assets) but it seems a fairly straightforward setup. Is there something I can do to make sure the bundle files get created early?
I got an issue with Django 1.6:
I want to change the default static file directory in django. I don't want it in
project/myapp/static but in project/static
I readed django's documentation, added
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIR =(os.path.join(BASE_DIR, 'static'),)
In my settings.py
Then I ran ./manage.py collectstatic and files copyed as expected.
Finally I launched the server, the app is using django boilerplate plugin, so my template begins with:
{% extends 'dh5bp/base.html' %}
{% load url from future %}
{% load staticfiles %}
{% block head %}
<link rel="stylesheet" href="{% static "css/homepage.css" %}">
{% endblock %}
And the Css won't load: But in my server log, I got that:
[29/Aug/2014 11:23:03] "GET /static/js/dh5bp/plugins.js HTTP/1.1" 304 0
[29/Aug/2014 11:23:03] "GET /static/css/homepage.css HTTP/1.1" 404 1657
As you see the statics file from dh5bp (Boiler plate plugin) Are loaded correctly, while the statics from my app aren't loaded correctly.
I tried to add + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to my urls.py, right after urlpatterns, It didn't worked.
So please if someone could tell me chat I'm doing bad and what should I change in the settings. It could be great
EDIT:
Did tryed the solution here, It give me the same result: only statics from boilerplate are loaded.
And not to mention that I've obviously checked if the files exists in /project/static, they does exists.
EDIT 2:
I tried to put the old static folder in my app, to ensure That he weren't looking for the files in the old folder. It doesn't, so I don't know where django expect those file to be? Is there a debug setup that could help on this?
Your STATIC_ROOT shouldn't be in STATICFILES_DIRS.
STATICFILES_DIRS should contain paths to your project's static files.
STATIC_ROOT is where all your static files are collected when you run collectstatic.
If you run django server with DEBUG=True, server will serve static files straight form STATICFILES_DIRS, and if DEBUG=False it won't handle static files at all. In that case you can run django server with option --insecure.
See this related question for more.
I am new to django. My project folder contain media folder. It contain some images.
How can I display this images in template of another app?
Path : mysite/media/blog/templates/blog/details.html
details.html
{{<img src="media/image_2.jpg">}}
But it does not display anything..
You should set-up the STATIC folder and to configure the media files.
The full reference: https://docs.djangoproject.com/en/dev/howto/static-files/ and also
https://docs.djangoproject.com/en/1.4/howto/static-files/.
However you should use something like this (once you configured properly the STATIC files) :
<img src="{% static "media/myexample.jpg" %}"/>
Edit:
Before using static tag, You must load specific tags incluing on top of template code: {% load staticfiles %} : Referring to static files in templates