models
image_url = models.ImageField(upload_to="uploads/shows",blank=True,null=True)
I need to upload the image at a particular directory. I am trying to add these media files using admin panel. How do I upload the images ?
Edit 1
http://192.168.2.9:8000/static/uploads/shows/myImage.jpg
Page not found (404)
Request Method: GET
Request URL: http://192.168.2.9:8000/static/uploads/shows/myImage.jpg
'uploads/shows/myImage.jpg' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Edit 2
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
The error does not say your upload failed ; it's saying it failed to find the file once uploaded. So like the doc explains about "Serving uploaded content", you also need to set MEDIA settings like they suggest to us.
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)
and then you will be able to upload your file too
Related
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)
I wanted to enable serving static files through django 1.5 builtin development server. I came across a strange problem.
If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), ", then it responds me with 304 , which is conditional get.
I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template.
For production I have used nginx to serve the static file so there is no problem.
My settings.py looks like
STATIC_ROOT = APPLICATION_PATH+"/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('vendors',APPLICATION_PATH+'/vendors'),
('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
My application urls.py looks like
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any hints would be highly appreciated.
Thank you .
please read this first
https://docs.djangoproject.com/en/1.5/howto/static-files/
There are some settings related to static files serve
The list of finder backends that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Absolute path to the directory static files should be collected to
STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')
URL prefix for static files.
STATIC_URL = '/static/'
You can specify Additional locations of static files
STATICFILES_DIRS = (
os.path.join(APPLICATION_PATH, 'vendors'),
os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)
There is special application for static serve in django contrib
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
After You have configured your project run collectstatic command
python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.
I am using ADL LRS to setup a LRS(Learning Record Store) system for my own use. It uses TIN CAN API. I am using ubuntu server
As the documentation states, For the setup of LRS, I need to install django adn set it up for LRS. The adl_lrs folder inside the ADL_LRS contains the setting file for django(settings.py). I am bit new to django, so I can not fully understand the meaning of this part of the file-
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/var/www/adllrs/media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://my-site-name.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
It states-
1. MEDIA_ROOT = '/var/www/adllrs/media/' which I assume it means is to put the media files like songs and videos, at this location
2. STATIC_ROOT = '' which I assumes means the path of the static directory which contains the HTML, CSS, js files.
On cloning the git, I setup the LRS, which by the way started but all the CSS broken. I looked into DOM inspector, where the link of CSS files are like-
http://my-site-name.com:8000/static/admin/css/base.css
When I visited the above url to see what's happening, I got following output as HTML(same as I get when visiting homepage, i.e http://my-site-name.com:8000)-
Page not found (404)
Request Method: GET
Request URL: http://my-site-name.com:8000/
Using the URLconf defined in adl_lrs.urls, Django tried these URL patterns, in this order:
^XAPI/
^xapi/
^admin/
The current URL, , didn't match any of these.
My urls.py looks like-
url(r'^XAPI/', include('lrs.urls')),
url(r'^xapi/', include('lrs.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls))
Obviously I am not mentioning my home page in the urls.py which point to the error. So where should I put the CSS,JS files to get the broken CSS fixed and make a default home page for this? and also I have tried to send tin can statements from wordpress, but I could not get the statements on my server. can anyone tell me how to setup a proper ADL LRS on ubuntu.
PS- Do not tell me to read the documentation as I have done it like dozen times. Tell me where I am wrong in implementing the documentation.
It sounds like you have the LRS running with just gunicorn. gunicorn by itself doesn't serve static files like the JS and CSS. I found another SO page talking about serving static files with gunicorn: https://stackoverflow.com/a/12801140/1187723
As for the LRS, when I am developing and testing locally I run Django's test server, which will deliver static files. From the project's root directory ADL_LRS, start it with python manage.py runserver. https://docs.djangoproject.com/en/1.4/ref/django-admin/#runserver-port-or-address-port
When we deploy the LRS at https://lrs.adlnet.gov/xapi/ we use nginx, which is configured to serve the static files. We have some initial set up stuff about it at https://github.com/adlnet/ADL_LRS/wiki/Using-Nginx-for-Production
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".
The media is currently on my local development machine.
My MEDIA_ROOT, MEDIA_URL, ADMIN_MEDIA_PREFIX and are specified as below:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
MEDIA_URL = '/media/'
SITE_URL = 'http://localhost:80'
ADMIN_MEDIA_PREFIX = '/media/admin/'
There is no 'admin' folder but that shouldn't make a difference I don't think.
In the urls.py file I have:
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
I am at a loss as to what I should do to get it working.
[I am trying to learn django and am working with an existing project that's pretty hairy]
You're mixing and matching pre and post-Django 1.3 static file handling. Originally all static files were served from MEDIA_URL, but Django 1.3 introduced the staticfiles contrib package and the associated STATIC_ROOT and STATIC_URL settings. django.views.static.serve utilizes the new staticfiles app, which you haven't set up.
Assuming you're running Django 1.3, first, you'll need to add 'staticfiles' to your INSTALLED_APPS. Then, you'll need to define STATIC_ROOT and STATIC_URL. The standard location is a project-root level directory named "static".
You'll also need to add the staticfiles template context processor:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
)
This will make the STATIC_URL variable available in your templates, so you can reference your resources with something like {{ STATIC_URL }}css/style.css
All your static resources will also need to go into an app(s)-level directory named "static". The actual project-root level "static" directory is never directly used. It's simply the place where the collectstatic management command dumps all your static resources for use in production.
If you want project-wide static resources (not tied to any one particular app), you'll need an entirely separate directory (i.e. not the same as MEDIA_ROOT or STATIC_ROOT). I tend to use one named "assets". You'll then need to tell Django to look in here for static resources as well with the STATICFILES_DIRS setting:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'assets'), # or whatever you named it
)
MEDIA_ROOT/MEDIA_URL are now only used for user uploads (e.g. any file created through FileFields and ImageFields, so you still need it, but you won't ever manually store anything there.
When you reach production, your webserver will need to serve both MEDIA_ROOT and STATIC_ROOT at MEDIA_URL and STATIC_URL, respectively. You'll also need to run:
$ python manage.py collectstatic
To make Django compile all your static files into the directory specified by STATIC_ROOT.
works with django 1.8 - 1.11:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development
note that Django documentation states that this is
not suitable for production use
(obviously unless you use if settings.DEBUG: part)
On development server, this page may help you.
https://docs.djangoproject.com/en/1.2/howto/static-files/
By adding follow code to urls.py:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
)
With python-django 1.7 I used
if settings.DEBUG:
urlpatterns = patterns('',
(r'^$', 'blenderx3d.first_step.views.index'),
(r'^media/(?P<path>.*)$','django.contrib.staticfiles.views.serve'),)