0

I have read other topics on stackoverflow and on the django website, but I am still confused.

These are my configurations:

STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
STATIC_URL = '/static/'

When I run manage.py collectstatic , my static files will be collected at myproject/assests/

I will store a default profile photo (for using with my models) here: myporject/static/images/default_profile_photo.png, so when I run collect static, it will be coppied in myproject/assets/images/default_profile_photo.png

Everything fine so far.

On my models.py I have a field:

class UserProfile(models.Model):
    photo = models.ImageField(upload_to = get_upload_file_name,
                              storage = OverwriteStorage(),
                              default = 'path/to/my/default/image.jpg'))

My main question is: what path should I set to this default atribute? should I benefit from the STATIC_URL somehow?

Other questions: How can I user here the STATIC_URL from settings.py? and what is the usage of STATIC_URL? where and how can I see the effect between using STATIC_URL='/static/' and STATIC_URL='/wtf/'

Mihai Zamfir
  • 2,167
  • 4
  • 22
  • 37

2 Answers2

0
   os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), 'static', 'templates'),

for each "os.path.dirname( )" I have one more within the following, which corresponds to a parent directory, so in my example I have four of them, it clarifies the root folder as being 4 folders above the current directory.

Ethan
  • 1
  • 2
  • with what you said, this is how it looks in my case: return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'assets', 'images', 'generic_profile_photo.png'). I'm still hard-coding the 'assets' word. Moreover, my default image is not loaded. I am getting "UserProfile matching query does not exist" when I try to take the picture fomr the DB: source_file = UserProfile.objects.get(user = request.user).photo – Mihai Zamfir Mar 22 '14 at 13:57
0

Files that are uploaded always go in a subdirectory of MEDIA_ROOT and are referenced from MEDIA_URL.

For any FileField (like ImageField), you simply give the subdirectory that will be used to store the files that are uploaded to this model.

If you have MEDIA_ROOT as '/foo/bar/uploads/', then in your models you have this:

class Foo(models.Model):
    photo = models.ImageField(upload_to='photos')

Then all photos uploaded will be stored in /foo/bar/uploads/photos/.

Unlike collectstatic, there is no command to manage media files. You have to deal with them on your own.

Now on setting a default value; in addition to a path, you can add the name of a callable (a function), that is called whenever an instance of the object is saved. You can exploit this:

import os

from django.contrib.staticfiles import finders
from django.config import settings


def get_default_photo_path():
   results = finders.find('/default/image.jpg')
   if results:
       return os.path.join(settings.STATICFILES_DIRS[0], results[0])

class Foo(models.Model):

    photo = models.ImageField(upload_to='photos',
                              default=get_default_photo_path, null=True)

Now, whenever a new object is created, the default value is the static file path to /default/image.jpg. If for some reason this file doesn't exist (for example, you forgot to add it), None will be returned. In other to store that, you have to set null=True.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I get an error when trying to do manage.py runserver: The joined path (C:\images\default_profile_photo.png) is located outside of the base path component. I'm doing results = finders.find('/images/default_profile_photo.png') . And is supose this results will have 2 elements (because it finds the image under /assets/images, ass well the one under /static/images) – Mihai Zamfir Mar 22 '14 at 14:11
  • `find` will return the first that it finds, but you have to append the path to the `STATICIFILE_DIRS` directory from where the file was found. I have updated the example, to show how to append the first item in the `STATICFILE_DIRS` tuple to return a complete path. – Burhan Khalid Mar 22 '14 at 14:20
  • still not working. has the path to be the abosulte path? cause if so, I did default = os.path.join(settings.STATIC_ROOT,'images','generic_profile_photo.png') – Mihai Zamfir Mar 22 '14 at 15:42