0

Using IBM Cloud Foundry service for deployment, I am trying to find a way to serve static/media files with Django in DEBUG=False mode. When deploy, I get a fully functional app with working js and css except media files. My configs:

manifest.yml

  applications:
  - name: onlinecourse
    routes:
      - route: $HOST.$DOMAIN
    memory: 128M
    buildpack: python_buildpack
  - name: onlinecourse-nginx
    routes:
      - route: $HOST.$DOMAIN/static
    memory: 64M
    buildpack: staticfile_buildpack

Notice second app 'onlinecourse-nginx' serves static via '$HOST.$DOMAIN/static' and use pre-configured staticfile_buildpack with nginx start up somewhere inside, so probably that is a 'devil' here. (I run 'python3 manage.py collectstatic' beforehand, by the way.)

models.py

class Course(models.Model):
    ...
    image = models.ImageField(upload_to='course_images/')
    ...

settings.py

INSTALLED_APPS = [
....
'django.contrib.staticfiles',
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media')
MEDIA_URL = '/media/'

urls.py

urlpatterns = [
    ....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

some template:

<img src="{{ course.image.url }}" width="240px" height="240px" ...
                             

This gives me a link as '/media/course_images/...', and it works fine in DEBUG=True. But in DEBUG=False actual desired link is '/static/media/course_images/...' as nginx is configure with manifest.yml. How to meet them up?

My ideas:

  1. Explicitly play around with Django {% static ... %} construct and STATICFILES_DIRS config. It partially solves the problem giving /static/ prefix. But links become broken in DEBUG=True.

  2. It seems obvious to make some adjustments to nginx. But I don't see in staticfile-buildpack docs any straightforward way to feed some custom configs.

  3. Crazy way to let Django serve static even in DEBUG=False mode by himself with some ugly tricks like 'runserver --insecure'. I find it creative but probably avoidable.

Am I missing something?

  • Please less lyrics, more specifics. https://stackoverflow.com/help/how-to-ask Currently this is not a question but an invitation to discussion. It does not work like this on SO. Such unfocused questions are treated offtopic on SO and usually get closed until revised by author. – Ivan Starostin Jul 03 '22 at 18:13
  • I have trimmed the first part of your question with only intent to make it more focused and answerable. Feel free to revert my changes if you think they are harmful to your question. But my suggestion is to prune the second part badly as well and leave only important facts. If you want your question to be answered of course. – Ivan Starostin Jul 03 '22 at 18:16
  • I dont get this part `I get ... instead of ...` and then again `..the actual nginx link via... is alive` . Your config says media files must be under `/media/` URL. Not `/static/media/`. – Ivan Starostin Jul 03 '22 at 18:19
  • Thank you, Ivan! I refactored the question to be more targeted and less lyrical (with url explanation). Being immersed with the issue, it is hard to prune some extra info... Hope the edits will be more answerable, glad to see experienced point of view! – ILia Gridnev Jul 04 '22 at 09:36
  • I'm afraid now it has become an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) `desired link is '/static/media/course_images/...' as nginx is configured` - why would you configure nginx like that? If you want to marry Django and Nginx configs then you need to alter either of them: modify MEDIA_URL = '/static/media' or fix your nginx conf to catch `/media` requests. The second option looks more appropriate. Media files should not be mixed with static files. I know nothing about ibm-cloud but perhaps you need another `route` for media files/urls. – Ivan Starostin Jul 04 '22 at 10:50
  • Skillful remark, Ivan! Actually I got stuck with solution (Y), so **had** to fully describe the question (X) to be consistent. Yes, a lot of words provided more generic impression. But wish to believe they have a value because it seems to be ibm-cloud specific. – ILia Gridnev Jul 04 '22 at 15:24
  • As for your suggestion for nginx conf, it sounds nice as I already pointed in 2nd idea. The problem is deploying to IBM Cloud Foundry means to create 'apps' (not a fully manageable containers to wrap native nginx, etc), so I don't know how to customize nginx inside (only set a route to app) and that is probably a core issue here. All other staff is provided as a reference to an approach of handling the problem with Django capabilities, as I`ve seen in many related topics. – ILia Gridnev Jul 04 '22 at 15:25
  • If you want to ask a question about configuring nginx in ibm-cloud - ask this question. Django has nothing to do with ibm-cloud configuration. If you have a question about configuring Django media files - ask this question. ibm-cloud configuration has nothing to do with Django internal settings. Currently your _story_ has two close votes for `needs more focus` reason. After the third one this question will be closed. If you want someone to join debugging or consulting on any of mentioned products - I'm afraid that is not what SO is for. Please follow the first given link "how to ask". – Ivan Starostin Jul 04 '22 at 15:51
  • Ok, It seems I got my eye blurred with that topic... I appreciate SO community and just tried to be descriptive as possible, so slipped out of sight all you are talking about, my bad. Actually your clarifying remark kicked me to try to refactor the _story_ from scratch to specifics. On that way I finally got appropriate solution! Some misleading understanding of _buildbacks_ and rearranging project structure with URL edits. Will definitely try more flexible _nginx-buildpack_ next time. Do I have to close my question somehow or share insights? Thank you for attention, it was beneficial! – ILia Gridnev Jul 05 '22 at 14:25

0 Answers0