Django - serving user uploaded images - python

I am having some problems with serving user uploaded files from my Django application:
from models.py:
class Picture (models.Model):
title = models.CharField(max_length=48)
date_added = models.DateTimeField(auto_now=True)
content = models.ImageField(upload_to='pictures')
From the Django admin the files get uploaded to the user_res/pictures/ folder.
from the project's settings.py:
MEDIA_ROOT = 'user_res'
MEDIA_URL = '/user_res/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Every time I try to reference a static resource (namely css or js files), everything works fine using URLs such as
http://localhost:8000/static/<subfolder>/main.css.
However, I cannot access user uploaded files (which get created by the admin interface in the user_res/pictures folder with a relative URL such as
user_res/pictures/test.jpg
the URL is dynamically created with this line of code from a Django Picture model callable:
return '<img src="{}"/>'.format(self.content.url)
I have no dedicated url-s for either static or media files in the url.py file.
Does anybody have any idea as to how to make Django serve the media files? I understand that for live environments I will need to configure an http server to serve that particular directory, but for now I want to maintain a lightweight development suite.
Thank you.

Edit your urls.py file as shown below.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
edit your projects settings.py to look like:
#Rest of the settings
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Please read the official Django documentation about serving files uploaded by a user carefully. Link to docs: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user

I think the url attribute returns a relative URL ( Django's FileField documentation ), so you should have:
return '<img src="{}"/>'.format(MEDIA_URL + self.content.url)
Relative URLs won't work, as a user visiting "http://localhost/books/" would be requesting "http://localhost/books/user_res/pictures/test.jpg".

Related

Retrieving images uploaded by users in Django Templates

I understand there are multiple (if not 100s) of questions pertaining to my issue. After trying all, I am here finally to ask. I am able to upload the image and the image path in the model. Example of an image field from model:
<ImageFieldFile: static/image1_FzGpiKx.jpeg>
My static folder is right where the project and app folders are. In similar hierarchy. I have the following settings in my settings.py file:
MEDIA_ROOT = '/static/'
MEDIA_URL = '/'
In my app level urls.py, here is what I have for rendering these images:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After all this settings , my template shows the following as img src:
/static/image1_FzGpiKx.jpeg
Here is how I render in template:
<img src={{article.image.url}} />
Yet it just renders the typical broken image icon. Can someone help me here? Thank you!
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/static/'
use this in your settings file, and upload a new image and then check it

How to download file from django admin

I created FileField in class Part in models.py. Also I get the link of this file by method file_link(self):
origin = models.FileField(upload_to='origin_files', null=True, blank=True)
def file_link(self):
try:
return "<a href='%s'>download</a>" % (self.origin.url,)
except PageNotAnInteger:
return "<a href=127.0.0.1:8000/main>download</a>"
file_link.allow_tags = True
In admin.py I added readonly_fields for file_link. Also I added in fieldsets file_link:
readonly_fields = ('file_link',)
fieldsets = (
('Характеристики плёнки', {
'fields': (....'data', 'origin', 'file_link')
}),
)
So, in my django admin I have FileField which allows to upload files to directory origin_files and link to this file. But when I click on this link I'm redirecting to home page of my site. For example, I downloaded file presentation.pptx. When I get url of this file I get
http://127.0.0.1:8000/admin/description/part/12/change/origin_files/Presentation.pptx
How I can download this file?
You have to configure MEDIA_URL in your django settings
https://docs.djangoproject.com/en/2.0/ref/settings/#media-url
Django builds your url the following way:
settings.MEDIA_URL + "origin_files/Presentation.pptx"
the MEDIA_URL default is an empty string, therefore you get the url "origin_files/Presentation.pptx" and your browser concatenates it to the current page, because the url doesn't start with a slash.
so you have to set the MEDIA_URL for example to '/media/'
then everything will work in your development environment.
On remote server this also requires configuring the web server appropriatelly for it to serve your MEDIA_ROOT to MEDIA_URL (out of the scope of this question obviously).

How to serve media files on Django production environment?

In me settings.py file :-
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
LOGIN_URL = '/login/'
MEDIA_URL = '/media/'
In my urls.py file:-
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
When I am uploading the profile image then it is uploading to specified folder. but when i am visiting the user profile url then i am getting error like this in terminal
"GET /media/profile_images/a_34.jpg HTTP/1.1" 404 103
a_34.png is present in /media/profile_images/
then why it is not showing on browser and i am getting 404 error?
Django is not made to serve media file in production environment. You must configure the STATIC_ROOT settings and alias webserver to directly serve.
For example
If you are using apache web server in production, add the below to your virtualhost configuration
Alias /media/ /path/to/media_file/
<Directory /path/to/media_file/>
Order deny,allow
Allow from all
</Directory>
If you use Nginx you would have to use something like
location /media {
alias /path/to/media/file; # Change to your own media directory here.
access_log off;
}
Alternatively, you could also serve static files from AWS S3 or other cloud servers using django-storages
Django discourages to serve media files on production from the server. Use cloud services like amazon s3 to server your media files. See this Django doc serve media then give that path in MEDIA_URL.
You can use S3 Amazon for static and media files. It will be better.
Problem with S3 Amazon
Making the S3 bucket appear as part of the file system has terrible performance and fails randomly. When we are copying a lot of files it can take 10, 15, or 20 minutes for the copying to complete making deployments take a long time when they don’t need to. If we send these directly into S3 the same copy command takes about 1 minute to complete.
Solution
Subclass S3BotoStorage twice, one class for static files and the other for media files. This allows us to use different buckets and subdirectories for each type. (see: custom_storage.py)
Update settings
1. AWS_STORAGE_BUCKET_NAME needs to be bucket to hold static files and media files
2. MEDIAFILES_BUCKET
3. MEDIAFILES_LOCATION
4.DEFAULT_FILE_STORAGE
5.STATICFILES_BUCKET
6.STATICFILES_LOCATION
This is the subdirectory under the S3 bucket for the app
7.STATIC_URL
8.STATICFILES_STORAGE
Create custom_storage.py with the contents:
from django.utils.deconstruct import deconstructible
from storages.backends.s3boto import S3BotoStorage
from django.conf import settings
#deconstructible
class StaticS3Storage(S3BotoStorage):
bucket_name = settings.STATICFILES_BUCKET
location = settings.STATICFILES_LOCATION
#deconstructible
class MediaS3Storage(S3BotoStorage):
bucket_name = settings.MEDIAFILES_BUCKET
location = settings.MEDIAFILES_LOCATION
Sample settings.py.tmpl for updates settings (as mentioned above) based on my stack.json
MEDIAFILES_BUCKET = '<%= #node["apps_data"]["aws"]["buckets"]["bucket-name"] %>'
MEDIAFILES_LOCATION = 'folder_name_for_media_files_in_bucket'
DEFAULT_FILE_STORAGE = 'custom_storage.MediaS3Storage'
# If we're not using our S3 backend storage we need to serve the media files via path
if DEFAULT_FILE_STORAGE == "custom_storage.MediaS3Storage":
MEDIA_URL = 'https://%s.s3-website-us-east-1.amazonaws.com/%s/' % (MEDIAFILES_BUCKET, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATICFILES_BUCKET = '<%= #node["apps_data"]["aws"]["buckets"]["bucket-name"] %>'
STATICFILES_LOCATION = 'folder_name_for_static_files_in_bucket'
STATICFILES_STORAGE = '<%= #node["deploy_data"]["project_name"]["django_static_files_storage"] %>'
# If we're not using our S3 backend storage we need to serve the static files via path
if STATICFILES_STORAGE == "custom_storage.StaticS3Storage":
STATIC_URL = 'https://%s.s3-website-us-east-1.amazonaws.com/%s/' % (STATICFILES_BUCKET, STATICFILES_LOCATION)
else:
STATIC_URL = '/static/'
load static from staticfiles Django Template Tag
Change all uses of {% load static %} in templates to {% load static from staticfiles %}
The “static” from static files can make use of different back ends for files, including an S3 back end or local file back end. Using “load static” uses the Django template tags library which doesn’t handle different back ends.
Use this in the templates when including a static file and after including “static from staticfiles”:
{% static “path/to/the/file.ext” %}
This will figure out the full path to the file or if it’s in S3 it will insert a full URL to the file.
Example
<link rel="stylesheet" type="text/css" href="{% load static from staticfiles %}{% static "css/style.css" %}”>
Useful info
“django.contrib.staticfiles.storage.StaticFilesStorage” is the default Django static files backend
References
https://docs.djangoproject.com/en/1.9/howto/static-files/
https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/
For nginx it works for me with the following configuration lines:
location /media {
alias /home/ubuntu/speedy-net/media; # Change to your own media directory here.
access_log off;
}
Also see my related question on Code Review.
You need to setup a server to serve static content on production. When only Debug is True, static content is served by Django. So you need to
1) Setup a server
2) Point server media path to STATIC_ROOT directory
3) Run collectstatic command of django to collect all the static files to STATIC_ROOT.
Please refer
https://docs.djangoproject.com/en/1.10/howto/static-files/
The following method worked for me:
I added the following configuration in Apache config file:
alias /media/ /path/to/media/
<Directory /alchemus/django/WebForm/media>
Require all granted
</Directory>
settings.py file contained the following settings for MEDIA:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Make sure you have the following settings in urls.py:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
The above setting is to make sure that django server only serves media files during development, serving media files in production should be handled by Apache server.
References: https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/#serving-files
Just add this code in urls.py
urlpatterns = [
.........
.........
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Django Rest app not using media root correctly to serve images

I have a project that has 3 apps, one named stores with store models, products with product models, and api which is the rest framework app that serves the Json results to clients. I set the media root in settings.py as MEDIA_ROOT = '/photos/' and the upload works for both product and store models. The main problem here is that for some reason the rest framework returns a url that references the api app instead of the products or stores apps for the media root url. here are my models
class Product(models.Model):
def get_image_path(instance, filename):
return '/Products/' + filename
picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)
store:
class Store(models.Model):
def __str__(self):
return self.name
def get_image_path(instance, filename):
return os.path.join('productphotos', 'stores', filename)
picture = models.ImageField(width_field=None, max_length=100, blank =True, null =True)
How do i set the mediaroot to the project directory instead so that all apps in the project reference it as mediaroot instead of themselves?
The upload works and upoads the pictures to the instructed directories in the root of the project (where manage.py is found), but the rest framework thinks it should get the media from the api app.. what's the proper way of doing this? here are screenshots:
the path uploaded to
the path returned in json
The MEDIA_URL setting is the URL path in the browser. The MEDIA_ROOT setting is the root directory on your server, and should be an absolute path.
MEDIA_URL = '/pictures/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_pictures')
Also, if you want Product and Store pictures to go into different sub directories, for example pictures/products/ and pictures/store/, you'd need to set the upload_to argument on the model field. For example
picture = models.ImageField(upload_to='products/', ... )
Edit: To serve static and media files during development, add this at the end of the urls.py
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

ImageField in Django 1.5.4 -- dealing with the images' paths

I am using Django 1.5.4 and I have a project with the following structure:
django_site
-django_site
-books # the app
-media
-images
-books
-authors
-static
-images
-templates
This is the necessary code:
# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'
# models.py
class Book(models.Model):
image = models.ImageField(upload_to="images/books/")
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^books/$', 'books.views.get_all_books'),
)
My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.
The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.
How do I fix that? Looking forward to your responses.
Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT
As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.
You can also have a look at the docs regarding this question.

Categories