I'm having an issue with Django-Filebrowser when I try to upload an image. I'm using the following versions: Django 1.8.7, Filebrowser 3.6.4 and Python 2.7.
When I try to upload an image, everything works as expected, and the uploaded files are placed in the right folder without any problem; but the resultant file url doesn't have any leading slash necessary to serve properly the file.
For example, if I try to upload "test.jpg", the final image url will be "uploads/test.jpg" and "_versions/test_xxx.jpg" that actually creates issues even in the Filebrowser panel, since the resulting request will be issued to http://localhost:8000/admin/filebrowser/browse/uploads/test.jpg and http://localhost:8000/admin/filebrowser/browse/_versions/test.jpg rather than http://localhost:8000/uploads/test.jpg and http://localhost:8000/_versions/test.jpg that the server would serve properly.
(Note that the Filebrowser panel url is http://localhost:8000/admin/filebrowser/)
Here's my settings:
urls.py:
urlpatterns = [
url(r'^admin/filebrowser/', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('pages.urls')),
url(r'^blog/', include('articles.urls')),
url(r'^tinymce/', include('tinymce.urls')),
url(r'^grappelli/', include('grappelli.urls')),
]
settings.py
FILEBROWSER_DIRECTORY = 'uploads/'
FILEBROWSER_VERSIONS_BASEDIR = '_versions/'
Add a leading slash to the FILEBROWSER_DIRECTORY would trigger a SouspiciusFileOperation error since '/uploads/' is located outside the project folder.
Thanks in advance.
Finally I've got the point. It's enough to set explicitly MEDIA_ROOT and MEDIA_URL:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Now Filebrowser will search for "uploads/" and "_versions/" folders in this new parent one, so "/media" needs to be served accordingly.
Related
I am currently trying to configure my django project so that I can access static file images via url. However, it is currentely saying that the image does no exist.
Here are my settings for static files:
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, '/static/images')
Here are my base project urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls'))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
And my folder structure is, in the root of my project: static->images->[all images in here]
Upon trying to access an image via url like this: http://127.0.0.1:8000/images/*proper image name*
It tells me that the image doesn't exist. Here is the call back:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/IMG_4622_2.JPG
Raised by: django.views.static.serve
'IMG_4622_2.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.
Does anyone know why this may be happening?
STATIC_URL specifies the url path to serve static files. In your case the setting points to /static/.
So that's where you have to access them - http://127.0.0.1:8000/static/images/*proper image name*
I know there are many answer already, but unfortunately I failed to find a solution on them.
Basically I am trying to serve image files stored at server|/srv/post/users/folder/PIMAGES/ACTIVE/image.jpg to client, but with the configuration I have in my settings.py file, I am getting an http 404 file not found error.
my settings.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = '/srv/post/'
MEDIA_URL = '/post/'
and then if I refer to http://127.0.0.1:8000/post/users/folder/PIMAGES/ACTIVE/image.jpg
I just get 404 page not found.
#whocaresz you are referencing your MEDIA_ROOT and MEDIA_URL incorrectly, when you specified MEDIA_ROOT as srv/post/ then your image.jpg should be in post directory and to access it go to http://127.0.0.1:8000/post/image.jpg
for future students, I also had to add a static entry to my project urls.py file as below:
urlpatterns = [
url(r'^thisischanged/', admin.site.urls),
path('', include('someapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
or + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) at the end.
I want to make web site which show file tree in web and print image/video on web.
I'm planning to print it with html tag img and video. However there are some problems with path. Most of my static files( like video or img) outside of django project folder.
For example django template source and view source is at /Users/Knight/My-site/Damotorie_cafe/views.py and image is at /Applications/MAMP/practice
I tried to get relative path with python code and use it for src attribute.
Same path works well when I don't use django and just open it with local web browser, however when I open it with django then error occur.
I'm starter in django. Can any one help me?
(I searched it before asking, but most of question is about upload not show image.)
You need to configure your static files in Django. In the settings.py, you need to set up:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/path/to/your/images/outside/web/tree/'
]
STATIC_URL = '/static/'
In development, you need to set this in your base urls.py file:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login', auth_views.login, name='login'),
...
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then in your template files, first set this at the top of the template file, so you load the 'static' tag:
{% load staticfiles %}
Then you use the static variable:
<img src="{% static 'path/img' %}"/>
The trick is, Django searches through STATICFILES_DIRS to find that 'path' in your template file, so the end 'path' has to be different from anything you have in your other listed STATICFILES_DIRS. It searches the list in order, and will find the first one that matches, in any event.
I am ashamed to ask a question of that sort but I still can not solve my problem. I normally uploaded image in my media directory and can see image link in my admin but if I click on link I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/photo/img/9.jpg
Using the URLconf defined in TeamStudy.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, photo/img/9.jpg, didn't match any of these.
my project structure:
src
static/
photo/
img/
settings:
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'photo')
MEDIA_URL = '/photo/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'
urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
I suspect it perhaps quite simple and I miss something. Please point me.
You need to add the media MEDIA_ROOT and MEDIA_URL in your urlpatterns
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
check following links for more details
Accessing "Media" files in Django
Django classifies user files in two types
Static files
Media files
this link helps you understand the difference between them
Your issues deals with Media files.
In future development, you may need to serve static files, to serve them you will need to add
STATIC_ROOT and STATIC_URL to the urlpatterns in a similar way that MEDIA_ROOT and MEDIA_URL are added
Change your URL's patterns:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
for more info Django Docs
First point: don't put uploaded medias in you static directory - static is for static files that are part of your project (css, js etc) -, you want to use MEDIA_ROOT and MEDIA_URL for this.
wrt/ serving the static and media contents, it depends on the environment. On your local dev environment you want to use the staticfile.views to serve both static and medias as mentionned in the other answers, but don't do this in production : on a production server, you want to use your front web server to serve static (and media) contents.
hi all
Im tryng to integrate a tinymce in a django admin page.
I've installed the django-tinymce module (http://code.google.com/p/django-tinymce/)
I followed the instructions so these are my files:
settings.py
INSTALLED_APPS = (
...
'tinymce',
)
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,paste,searchreplace",
'theme': "advanced",
}
TINYMCE_SPELLCHECKER = False
TINYMCE_COMPRESSOR = False
url.py
urlpatterns = patterns('',
# Example:
# (r'^uboPy/', include('uboPy.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
)
i have the tinymce's js in a folder in the root called media/js
In the model i've this line: text = tinymce_models.HTMLField()
when i run the server i don't get error but when i go in the admin area of my model the tinymce is not loaded. With firebug i see that the tinymce library give a 404 error but the path is correct.. i have some problem in my url.py?
thanks for the help
Are you just putting a folder in your project root called media/js? It actually needs to be served somehow from a url somewhere.
The JavaScript file is loaded via your browser, so it needs to be accessible to the internet where all of your other media lives. This part has nothing to do with django, so you shouldn't expect errors or URL issues.
Once you've solved that part, specify the URL where the file can be accessed via the TINYMCE_JS_URL settings parameter.
For example, on my setup, the js is..
TINYMCE_JS_URL = '/media/js/tiny_mce/tiny_mce.js'
With firebug i see that the tinymce
library give a 404 error but the path
is correct..
Are you saying you can visit this URL path and the JS file loads? What I'm saying is: how is the path correct if it's a 404?
I found out the django-tinymce documentation is outdated, i.e. partially wrong.
What I discovered is that different versions of tinymce and django-tinymce packages are not compatible.
I solved it adding some variables to my project/settings.py and altering the tinymce directory and file names.
django-tinymce urls.py had some hardcoded paths in it which assumed the directories were named "tiny_mce" when in reality they were named "tinymce", hcen I had to rename them, or change the django-tinymce urls.py.
# project setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_JS_DIR = os.path.join(STATIC_DIR, "js")
TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")
#TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
#TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")