Wagtail Images Not Loading Once Deployed - python

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.

Related

Error loading image filepath from the database in django

i am starting to use django and i have a little mistake in some of my code but i dont know where is it.
I am trying to load a image from my database in a filepath.
This a part from my models.py that i am using to load the image:
class Articulo(models.Model):
...
nombre_imagen=models.CharField(max_length=64)
As you can see, i am loading the image from a filepath in my database.
And this is the part of the code that i am trying to do that:
{% if articulo %}
{% for articulo in articulo %}
....
<img class="card-img-top" img src="{% static '{{articulo.nombre_imagen}}'}" alt="">
.....
{% endif %}
The image is located in the static files:
STATIC_URL = '/static/'
I think there is a stupid problem, but as i said before. I am new at django.
I hope that anyone can help me, thank you!.
Your issue is that you aren't calling the image variable correctly.
src="{% static '{{articulo.nombre_imagen}}'}"
should be
src="{% static articulo.nombre_imagen %}"
Also, your image field should be models.ImageField().
nombre_imagen=models.CharField(max_length=64)
should be
nombre_imagen=models.ImageField(upload_to="images/", blank=True)

Defining Image location in settings file django

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.

flask-assets: Deployed pages need to be reloaded a few times before they work

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?

Change django's default static directory

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.

how to display images?

please help bring images into the template
in django1.6 I uploaded via the form in the database image. screenshot. ( checked that the images loaded in a specific directory ) . then imported into the template file settings.py variable BASE_DIR and to all records of a table . then tried in the template to display the image as follows:
{% for entrie in all_entries_carousel %}
<a href="{{ entrie.link }}" title="{{ entrie.title }}" target="_blank">
<img src="{{ BASE_DIR }}/{{ entrie.image }}" width="300" height="200" alt="{{ entrie.title }}" />
</a>
{% endfor %}
results in images that I have not loaded and displayed.
in the source path
c:\Python33\django_projects\proj1/carousel/media/images/img1.png
please tell me how can I still display the image. Sure , there is a path without importing BASE_DIR
ps this way does not work proj1/carousel/media/images/img1.png
You need to configure your static files.
From Django docs:
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, either hardcode the url like
/static/my_app/myexample.jpg or, preferably, use the static template
tag to build the URL for the given relative path by using the
configured STATICFILES_STORAGE storage (this makes it much easier
when you want to switch to a content delivery network (CDN) for
serving static files).
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
Store your static files in a folder called static in your app. For
example my_app/static/my_app/myimage.jpg.

Categories