WebFaction
Community site: login faq

Hi,

To create new article for my blog I use a form in my admin panel where I can upload Image and write text. To order all the files I use dynamic file path. The name of the path is the name of the slug of the article.

from django.db import models

def article_directory_path(instance, filename):
    return 'blog/static/blog/{0}/{1}'.format(instance.slug, filename)

class Article(models.Model):
titre = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)

image = models.ImageField(upload_to=article_directory_path)
[...]

But when I create new article the path is not created and images are not uploaded into my "STATIC_ROOT" ($HOME/yohan8/webapps/static_media/) or "BASE_DIR" ($HOME/yohan8/webapps/my_webfaction_project/my_project_name/). The new article exist but it doesn't load my image. When I create manually the path in STATIC_ROOT with image inside it works but I want to do it automatically.

asked 24 Feb '16, 14:55

yohan8
1316
accept rate: 50%

edited 24 Feb '16, 15:46


I made a confusion between MEDIA_ROOT and STATIC_ROOT :( But my same project in www.c9.io creates the correct path at the correct place without MEDIA_ROOT (and without STATIC_ROOT because DEBUG = True).

So now when I create new article I always must lunch ./manage collectstatic to send files into my STATIC_ROOT or can I do it in the same time as the botton "Save" in my admin panel ?

permanent link

answered 25 Feb '16, 11:53

yohan8
1316
accept rate: 50%

edited 25 Feb '16, 11:57

No, the correct solution would be to set MEDIA_ROOT in your settings, and configure your application to serve the files from there. That is the Django is supposed to work.

(25 Feb '16, 18:19) seanf

I set my MEDIA_ROOT to $HOME/yohan8/webapps/my_webfaction_project/my_project_name/ (it can't be the same as STATIC_ROOT) and now you said my project must search files in MEDIA_ROOT ? Django is not supposed to read files from the path of my project for security reasons, only with STATIC_ROOT (ref).

The deploying static files documentation speak about to use fabfile to send files to STATIC_ROOT automatically. I saw quickly is it a python2 tool and my project use python3. Am I in the correct way ?

(25 Feb '16, 20:19) yohan8

Django is not supposed to read files from the path of my project for security reasons, only with STATIC_ROOT

There is no requirement that MEDIA_ROOT is a location within your project directory. You can create a static-only app like /home/you/webapps/media_root and use that if you want (and in fact, that is what we recommend).

The deploying static files documentation speak about to use fabfile to send files to STATIC_ROOT automatically. I saw quickly is it a python2 tool and my project use python3. Am I in the correct way ?

If your project is using Python 3, like Python 3.4 or whatever, then use the appropriate version of Python when you run your management commands, eg:

python3.4 manage.py collectstatic

Note that collectstatic is only for managing static assets associated with installed apps. It has nothing to do with managing media uploaded as content.

(25 Feb '16, 20:25) seanf

You can create a static-only app like /home/you/webapps/media_root and use that if you want (and in fact, that is what we recommend).

The documentation says if I create static-only app to serve user-uploaded media files from MEDIA_ROOT it's not suitable for production use.

Whatever I try to create new application with MEDIA_ROOT so in my /home/you/webapps/ I have 2 applications (MEDIA_ROOT and STATIC_MEDIA), when I upload files with my admin panel it works (files go into MEDIA_ROOT) but when I ask the picture in my template it doesn't work. The path is good but the image is not load. Exemple :

`<img src="{{ MEDIA_URL }}{% static article.image1 %}" alt="img" /> {% endif %}`
`<img src="/media_root//blog/static/img.jpg" alt="img" />`

`<img src="/media_root{% static article.image1 %}" alt="img" /> {% endif %}`
`<img src="/media_root/blog/static/img.jpg" alt="img" />`
(11 Mar '16, 17:30) yohan8

The documentation says if I create static-only app to serve user-uploaded media files from MEDIA_ROOT it's not suitable for production use.

That documentation is referring to Django's django.contrib.staticfiles.views.serve() view. That is not what I'm referring to as a "static-only" application.

Here at WebFaction, a static-only application is created via our control panel at https://my.webfaction.com/applications/, using "Static" as the app category, and "Static-only" as the type.

Once you create that app, a corresponding directory will be created as a subdirectory of your /home/you/webapps directory.

You can then configure Django to use that directory as MEDIA_ROOT, and assign that application to your website using whatever URL path matches your MEDIA_URL.

(11 Mar '16, 22:36) seanf

Yes I did it and my two applications work perfectly but when I try to ask my files from my MEDIA_ROOT application in my template it doesn't work. The path of my image is webapps/media_root/blog/{article_name}/img.jpg

`<img src="{{ MEDIA_URL }}{% static article.image1 %}" alt="img" />`
`<img src="/media_root//static/blog/{article_name}/img.jpg" alt="img" />`

`<img src="/media_root{% static article.image1 %}" alt="img" /> {% endif %}`
`<img src="/media_root/static/blog/{article_name}/img.jpg" alt="img" />`

`<img src="{% static article.image1 %}" alt="img" /> {% endif %}`
`<img src="/static/blog/{article_name}/img.jpg" alt="img" />`

My MEDIA_URL is '/media/' and it match with the url of the application that I created (static / Static-only)

(12 Mar '16, 06:56) yohan8

Are you using the complete path for the MEDIA_ROOT or an absolute path, i.e.,

MEDIA_ROOT = "/home/you/webapps/media_root/blog/"

or

MEDIA_ROOT = "webapps/media_root/blog/"

Troubleshooting this kind of path issue can be easier if we can see the link in real-time. Is it possible to post the real URK here?

(12 Mar '16, 23:18) johns

I'm using the absolute path.

STATICFILES_DIRS = (BASE_DIR,)
STATIC_URL = '/static/'
STATIC_ROOT = '/home/yohan8/webapps/static_media/'
MEDIA_ROOT = '/home/yohan8/webapps/media_root/'
MEDIA_URL = '/media_root/'

When I save a new file I use this kind of path in my models.py

def article_directory_path(instance, filename):
    return 'blog/articles/{0}/{1}'.format(instance.slug, filename)

What do you mean with the link in real time ? The link of my settings.py ? I don't know what is a URK.

I there are a link between my MEDIA_URL and the url I chose in my webfaction panel when I created a new static-only application ?

(14 Mar '16, 08:08) yohan8

Regarding "URK", I believe he meant URL. What he's asking for is a URL for your live site that demonstrates the broken media URLs.

It might be easier for us to help you if you open a support ticket via the control panel.

(14 Mar '16, 15:42) seanf

You can find my site at http://yohan8.webfactional.com

(14 Mar '16, 16:04) yohan8

Ok, the problem seems to be that you're using the static template tag in all of your templates where you are trying to display uploaded media.

Instead, you should be using {{ MEDIA_URL }} to generate the first part of your media URLs.

(14 Mar '16, 18:50) seanf

Yes but like I showed in my previous post I use it but it doesn't work.

I need to use {% static article.image1 %} in my template because my path depend of the slug of my article. The slug is the title of my article but adapted for url.

When I create a new article I say to django to create a new folder in webapps/media_root/ with the name of the slug of my new article. All files is send to this new path (webapps/media_root/{slug_article_1}/) and django do it perfectly.

So when I call these files I must call for example article.image1 because only this variable know the path (with the slug) of my image1.

(15 Mar '16, 13:02) yohan8

The reason it didn't work as shown in your previous post is that you were including the static template tag, eg:

<img src="{{ MEDIA_URL }}{% static article.image1 %}" alt="img" />

Including the static template tag tells Django to inject the STATIC_URL into the rendered template.

That means you end up with a URL like http://yohan8.webfactional.com/media/static/blog/articles/media_root14/IMG_0012_500px.jpg which is why your images don't work.

If you leave out the static template tag, like this...

<img src="{{ MEDIA_URL }}{% article.image1 %}" alt="img" />

... then you should end up with the correct URL like http://yohan8.webfactional.com/media/blog/articles/media_root14/IMG_0012_500px.jpg

(15 Mar '16, 15:19) seanf
1

Perfect it works.

<img src="{{ MEDIA_URL }}{{ article.image1 }}" alt="img" />

And MEDIA_URL must be the same than the url I chose in my control panel when I created the application.

MEDIA_URL = '/media/'
(15 Mar '16, 17:19) yohan8
showing 5 of 14 show 9 more comments

The upload_to path gets appended to MEDIA_ROOT, not STATIC_ROOT (ref).

Check your MEDIA_ROOT location to see if the paths are being created there, and make sure you have your MEDIA_ROOT location symlinked and added to your site.

permanent link

answered 24 Feb '16, 23:06

seanf
12.2k41836
accept rate: 37%

edited 25 Feb '16, 18:18

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×40
×29
×2

question asked: 24 Feb '16, 14:55

question was seen: 3,372 times

last updated: 15 Mar '16, 17:19

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM