My model (models.py) has an ImageField field.

image = models.ImageField( upload_to=os.path.abspath(os.path.dirname(__file__))+'/static/media/products/' ) 

In settings.py:

  _PATH = os.path.abspath(os.path.dirname(__file__)) path = str(_PATH[0:-4]) PATH = "username.pythonanywhere.com" MEDIA_ROOT = os.path.join(path+"/pay/", 'static', 'media') MEDIA_URL = PATH 

When I add a picture, it is successfully added where it is needed. However, its URL is not correct. This URL is correct and the picture is located on it http: //username.pythonanywhere/static/media/products/defualt.png However, the picture is saved with about this URL: http: //username.pythonanywhere/user/name/home/app/static /media/products/defualt.png

  • First, upload_to='products' and that's it. Secondly, the media directory can NOT be placed inside the static directory, these are different directories for different tasks - andreymal

0