I am using Django Rest Framework to upload images.
The upload is working fine and is getting uploaded under media folder.
But I am unable to make the media links browsable. It says Error 404.
To make them browsable these are the changes that I made.
In settings.py I added the following lines.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/media/'
In url.py I added the following lines
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
DEBUG is True in settings.py as it's not in production. It is on my local machine under development.
But when I am starting the server it is showing me the following error:-
ImproperlyConfigured :- Empty static prefix not permitted
Sorry guys! I found my own mistake. Instead of MEDIA_URL I was using STATIC_URL
Related
This is the first web application I'm developing by combining django and reactjs, in fact react is new to me.
Please if my approach is wrong I'm open for correction.
When developing with purely django, I find no difficulty with static and media files.
It is clear that the elements involved are the media root and static root,
respectively. In that case, all I need to do is to specify the static/media root in the
settings.py in the django app as shown below:
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/images/'
urls.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
However, when I combine django and react the configuration changes since django is configured
to look at index.html file in react, in which case an additional element called STATICFILES DIRS
is added to the seetings.py file to specify the path to react index.html. In fact, my django app
configuration looks like this when I combine django and react:
settings.py
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'build/static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/images/'
urls.py
urlpatterns = [
...
re_path(r'^.*', TemplateView.as_view(template_name='index.html'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'build')],
...
]
Everything works well as when I'm developing purely with django except that when I combine django and react in development the images I upload refuse to displayed
on the browser since any path that does not match with the specified paths in urlpattern are redirected
to react. re_path(r'^.*', TemplateView.as_view(template_name='index.html')) So the image path is being redirected. When I click the image link in the admin file, instead of
the image to get displayed, I'm redirected to react also. I have made research online but I can't find any resources
that explained properly how to configure the django static root and media root to be able to serve files (
like images uploaded to the database) content.
Please, do I display images and media on the browser? In production, I would like to serve static files of the
django-react app with aws s3 bucket
Check that you have added the build folder to installed apps
INSTALLED_APPS = [ build, ]
If DEBUG use STATICFILES_DIRS else use STATIC_ROOT. Add this code in the settings.py file.
if DEBUG: STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] else: STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In the urls.py file
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
Then run python manage.py collectstatic
You should also install whitenoise pip install whitenoise
I have some default images in my media folder in Djang. However, on my website, I cannot see the images being displayed and I get a 404 error.
Now my project folder tree is(project name is register):
register
-- live-static
---- static-root
---- media-root
------ defaults
and my media and static files settings are:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'live-static', 'static-root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'live-static', 'media-root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
And inside my urls.py in the same folder as wsgi.py I have:
urlpatterns = [
"""
My other urls
"""
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My static files are loaded and the server can retrieve them, however, the images that I added to my folders in media-root are returning a 404 error by the server. Why is this happening as I have already also set STATICFILES_STORAGE as well?
This is something I got hung up on with my first Heroku deploy. Since Heroku projects run on dynos, they eventually go to sleep if there is not a request within a certain period of time. When the dyno restarts, it does not save the uploaded data. Here is a link to some information from Heroku: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
What I ended up doing is setting up an Amazon S3 bucket for storage of these files. Dennis Ivy did a video explaining how to do this: https://youtu.be/inQyZ7zFMHM
Update your setting file like this
there is no need to change any thing else
in urls.py change the urlpatterns to. remove STATIC_ROOT etc
+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in setting.py like this. remove other file settings like STATICFILES_STORAGE
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
#if your media folder is in public_html which contain the media file
MEDIA_ROOT = '/home/yourusername/public_html/media'
STATIC_ROOT = '/home/yourusername/public_html/static'
#if your media folder is in other_folder which contain the media file
MEDIA_ROOT = '/home/yourusername/other_folder /media'
STATIC_ROOT = '/home/yourusername/other_folder /static'
I'm trying to upload images using Mongoengine ImageField.
But after uploading a test image,I got page not found by visiting http://127.x.x.x:xxxx/media/testimage.png.
Raised by: django.views.static.serve
"/Users/xxx/Documents/basedir/media/testimage.png" does not exist
myproject/settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
myproject/myapp/models.py
from mongoengine import Document, ImageField
class Image(Document):
image = ImageField(upload_to="")
filename = fields.StringField()
myproject/urls.py
from django.conf import settings
urlpatterns = [
#bla
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What did I do wrong?
You may want to have a look at this. It is fairly thorough and provides every step needed to configure static files. 404 Error for django serving static files. How to setup django to serve static files?. It appears you are definitely missing STATICFILES_DIRS and also have you ran python manage.py collectstatic command?
It would also be good to confirm you upload_to of the ImageField is also correct. You can verify this in the shell. python manage.py shell
I deployed my Django app on heroku. Every thing is working fine except displaying images. Any uploaded image is not displayed if DEBUG=False.
settings.py
DEBUG = False
ALLOWED_HOSTS = ['salma-blog.herokuapp.com']
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.dirname(myblog.__file__)+'/static/'
STATIC_URL = '/static/'
#upload images
MEDIA_ROOT= os.path.dirname(myblog.__file__)+'/static/myblog/images'
MEDIA_URL='/images/'
urls.py
urlpatterns=[
...
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
image tag in my template
<img alt="img" src="/blog{{image}}"></a>
Whitenoise uses static file finders when DEBUG=True, so you most likely have a problem with your static file collection process.
Update your STATIC_ROOT and MEDIA_ROOT settings to a recommended method, such as:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'images')
I have been breaking my head over this for a full day but can't figure out the problem. It happened after I copied my project from one machine to another.
Settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Mentioned 'django.contrib.staticfiles' in INSTALLED_APPS as well.
Folder structure :
Django-Projects (root)
project
app
static
css
home.css
js
manage.py
Template :
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/home.css' %}">
urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('app.urls')),
)
It throws an error in the console on opening the template :
GET http://127.0.0.1:8000/static/css/home.css
Failed to load resource: the server responded with a status of 404 (NOT FOUND)
What might be wrong here ? Please help me out. Much thanks!
In your settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'
Then in your template file:
<link rel="stylesheet" href="{{ STATIC_URL }}css/home.css">
With the directory root structure that you have shown, I think the above setting should work. Have not tested it though. Let me know if it works.
set DEBUG=True and see if it works .. this means, django will serve your static files, and not httpserver which in this case doesnt exist since you run the app locally.
I had researched this problem for a full day. This will be okay:
DEBUG = True
ALLOWED_HOSTS = []
Django default BASE_DIR will search static content for you. But in your case you changed the way before initial project. So for that in your case you have to change your BASE_DIR like this .. then only static file will serve correctly
BASE_DIR = os.path.dirname(os.path.abspath(__file__) + '../../../')
Updated:
I didn't see that comment. ! DEBUG = True only for development and You set as False so will django use STATIC_ROOT = 'staticfiles' to serve the static content on the production environment ... Thank you
You don't have to refer to STATIC_ROOT = 'staticfiles'
Just Like that:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
I had the same issue in Django.
I added to my settings: STATIC_ROOT = 'static/'
It was the only problem.