I am trying to upload an image to the server. No matter what I do, it doesn't appear to properly upload the image. Here is my field in the model:
image = models.ImageField(upload_to='uploads/images/staff', verbose_name='Staff Member Photo', help_text='Required Dimensions: Square, about 275px height/width')
Here are my settings in the settings.py file:
STATIC_URL = "/static/"
STATIC_ROOT = 'static'
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = 'static/media/'
I can't figure out what's going on--it's driving me crazy!!
You should use the absolute path in the MEDIA_ROOT property. For example:
MEDIA_ROOT = '/var/www/mysite/static/media/'
BTW, what error you get then uploading the image? Is it a path-related problem?
Related
I have a directory '/media/profile_image' where profile images uploaded by users are saved. In the template I built the url by using the user object
<img id="profile_image" src="{{ request.user.profile_image.url }}"></div>
# Model
def get_profile_image_filepath(self, filename):
return f'profile_image/{self.pk}/{"profile_image.png"}'
...
profile_image = models.ImageField(max_length=255, upload_to=get_profile_image_filepath, null=True, blank=True, default=get_default_profile_image())
...
which creates the correct url to the desired directory. But it doesn't show the image, why's that?
# rendered url
<img id="profile_image" src="/media/profile_image/1/profile_image.png">
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
dir
Dealing with Django and media files on development like this :
First install pillow
pip install pillow on your activated env.
Specify the MEDIA_ROOT variable in the settings file
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') or MEDIA_ROOT = BASE_DIR / 'media' in Django 3.
Tell Django to serve media file (in development mode)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in the project urls.py
file.
(Optional) Specify the MEDIA_URL variable too
URL that handles the media served from MEDIA_ROOT.
Exemple :
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media' # or os.path.join(BASE_DIR, 'media/') in Django 2
And in your model, you can define a media file like this :
class Profile(models.Model):
# Others fields
photo = models.ImageField(upload_to="photos/")
Here the upload_to is used to designate the location where the images assigned to the photo attribute will be saved on the hard drive for all instances of the model. If you don't specify a value for upload_to, the images will be saved to the root of MEDIA_ROOT.
This urls.py of base project
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This is the settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT=os.path.join(BASE_DIR,'assets')
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
This code is to add items to the database dynamically and i am not able to understand why is he adding the urlpatterns.
He is adding media urls to the url patterns. So for example if you had an image or video or something stored in your django project, you can use the browser to access these files at MEDIA_URL. In the settings.py, you are setting MEDIA_URL (where you can go in the browser '/media/'), to point to the contents of your MEDIA_ROOT (the 'media' folder)
An image in media_root does not get displayed.
My code is located at https://github.com/tomaszm-web/Django-ecommerce
Can you please help me to work out why the image does not get displayed?
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I think you are using code of older version but you have django 3. You can try this code.
I replaced
re_path(r'products/(?P<pk>\d+)/', ProductDetailView.as_view()),
re_path(r'products-fbv/(?P<pk>\d+)/', product_detail_view),
with
path("products/<int:pk>/", ProductDetailView.as_view()),
path("products-fbv/<int:pk>/", product_detail_view),
in urls.py file and it works now.
I am using django-oscar in my current project and trying to save the media files in an s3 bucket. I am adding the products in default dashboard in django-oscar.
I am confused about how to save product images on s3 and the how to save image url in Product model. In order to achieve this, what should I do? Do I have to modify Product model and add a new field for image urls? Or tweaking STATIC_URL and MEDIA_URL would be enough?
Here is my media configuration.
STATIC_URL = '/static/'
MEDIA_URL = "https://s3.amazonaws.com/<bucket_name>/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(STATIC_URL, 'media')
I'm using wysiwyg redactor for admin page. So I can add some images to my articles. Firstly, In settings.pyI wrote:
REDACTOR_UPLOAD = '/media/uploads/'
MEDIA_ROOT = '/media/'
MEDIA_URL = '/media/'
in this case, all uploaded images are in C:\media\uploads. It's working.
But I need images to be located in project folder. So I write:
REDACTOR_UPLOAD = os.path.abspath('/media/uploads/')
MEDIA_ROOT = os.path.abspath('media')
MEDIA_URL = '/media/'
Then image location: src="/media/C%3A/virtenvs/web/src/mysite/media/uploads/CAM00415.jpg"
But when debugging settings.py, MEDIA_ROOT = 'C:\\virtenvs\\web\\src\\mysite\\media'
Why?
You put a relative path in MEDIA_ROOT and an absolute path in REDACTOR_UPLOAD. Compare the following two in an interactive console:
>>> os.path.abspath('media/')
C:\\Users\\<username>\\media
>>> os.path.abspath('/media/')
C:\\media
C:\Users\<username> is the current working directory here. Unless you use an absolute path (starting with a /), the path in abspath will get appended to your current directory. The working directory of django seems to be C:\virtenvs\web\src\mysite, which also appears to be your project's directory, so in this case using a relative path for both settings should work:
REDACTOR_UPLOAD = os.path.abspath('media/uploads/')
MEDIA_ROOT = os.path.abspath('media/')
MEDIA_ROOT must be an absolute filesystem path, example:
MEDIA_ROOT = 'C://media/'
to make it inside project folder:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media').replace('\\','/')