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')
Related
I've deployed my Django application to the a2hosting web hosting the styles aren't showing. Also, the page that has swagger documentation is blank and doesn't have anything.
Here in the image, you can see how the Admin page is showing.
admin page
I've made collectstatic and here is the setting.py configurations:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
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)
I have some trouble when deploying Django to Google Cloud.
Currently, I'm using Django2.2.
The HTML icon didn't load correctly when I open it from my laptop.
But when open it using my mobile browser, it will show the icon perfectly.
Already try to clear cache and cookies but still doesn't work, not sure why.
The images and CSS in static/ directory can be loaded, but only the HTML icon that can't be load correctly.
I've followed this documentation itself.
And I think it's kinda funny cause I've tried to deploy using DEBUG = True and change the STATIC_URL = '/static/' but everything works perfectly.
But, as it says from Django documentation, that we need to change the DEBUG to False when in productions.
Here are my settings.py looks like:
DEBUG = False
ALLOWED_HOSTS = ['*']
...
MEDIA_ROOT = 'media/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static/')
STATIC_URL = 'https://storage.googleapis.com/djangodbs/static/'
# STATIC_ROOT = '/static/'
# STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/static/'
]
Here are some of the screenshot:
Desktop View
Mobile View
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?