0

In case I'm:

  1. at a very early stage of developing;
  2. using Chrome, which doesn't allow images from local resource, and
  3. storing my images in a local /static folder,

what could be the best workaround for displaying images, other than through the {% static %} tag?

I have been using {% load static %}...{% static variable/path %} in my templates, but now I'm trying to display images in the admin page, which requires a good deal of overriding, and admin templates aren't easy to find where exactly to override.
Lately I tried format_html, as in:

from django.contrib import admin

(...)

@admin.display(description='Foto')
    def show_foto(self):
        return format_html(
            '<img src=https://media.(...)>'
        )

Which works fine as long as the image resource isn't local.
Also, Django tags don't seem to work inside format_html().

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
101is5
  • 309
  • 2
  • 12
  • That is what Django does out of the box, just set your [config](https://docs.djangoproject.com/en/4.0/howto/static-files/) correctly. You don't need "local urls", you need to map STATIC_URL to STATIC_ROOT where both can be anything. Just go through the docs. – Ivan Starostin Jan 24 '22 at 17:22
  • @IvanStarostin You mean defining a path to STATIC_ROOT then setting like STATIC_URL = STATIC_ROOT? – 101is5 Jan 24 '22 at 18:17
  • @IvanStarostin I have gone through the docs over and over, and specially in the page you've linked to it says, at step 3 on Deployment section, to get a webserver to serve the files. This is not the case for me right now, because I want to use the files from my pc storage, which has been possible so far through the {% static %} tags in the templates I've written. However in the admin pages it seems a bit more complicated. I did manage to display a picture on the 'change list' using format_html though, but providing a link form an image server. – 101is5 Jan 24 '22 at 18:20
  • @IvanStarostin I used format_html in a method of a model, which I then provided to list_display in admin.py. As I said, when the source is a link to an image server, it works fine, but not with the files from the static folder. The code that works in my templates "{% load static %}" doesn't work inside format_html. "load static" returns a template rendering error, and without it, I get a broken picture (not found). – 101is5 Jan 24 '22 at 18:27
  • `I want to use the files from my pc storage` this is what everybody does during development without any crutches. `I used format_html` drop this code. `STATIC_URL = STATIC_ROOT` neither docs nor I said something like that. There are dozens of similar questions on SO, go through them, follow the docs, fix bugs. As said, Django does what you want out of the box, just complete a valid setup. – Ivan Starostin Jan 24 '22 at 18:52
  • 1
    You need to setup a valid config which will end with a mapping of STATIC_URL prefix to STATIC_ROOT folder where your files are which is what you want (to show local files on rendered pages). Urls are mapped to folders by Django engine, you don't need "file paths instead of urls" in templates. What you described is achieved by a minimal valid configuration of Django static files. `at step 3 on Deployment section` if you need `files from my pc storage` it means you are not **deploying** anything. Don't read that part (deployment) for a while. – Ivan Starostin Jan 24 '22 at 19:51
  • 1
    Review your settings.py and urls.py, revert templates to using `{% static %}` instead of your function, get a specific error, ask another question with providing: a) settings.py part for static files b) urls.py c) template code d) generated static url in rendered page e) folder structure with static files f) specific error message. After that someone will be able to give you more specific hint or a solution. – Ivan Starostin Jan 24 '22 at 19:55
  • 1
    one of such questions https://stackoverflow.com/questions/66437690/django-html-template-cant-find-static-css-and-js-files/66439076#66439076 – Ivan Starostin Jan 24 '22 at 20:04
  • @IvanStarostin I think handling static files is ok, I believe this isn't the issue. I do understand I'm still just rendering stuff, not deploying. By the way, I've just become fully aware that customizing admin pages has a right way to go: override a view method e.g. change_view, extend its context, then implement the new variables in the custom template(not quite like writing my own views and templates...). Thanks a lot for your time! – 101is5 Jan 24 '22 at 21:44

0 Answers0