Django - Serving Media ValueError - django

I'm trying to serve user uploaded media files in my dev environment.
#settings.py
#[...]
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = (os.path.join(SITE_ROOT, 'media/'))
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)
#[...]
#url.py
urlpatterns = patterns('',
#[...]
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
{'document_root', settings.MEDIA_ROOT}
),
url(r'^%s(?P<path>.*)$' % settings.STATIC_URL[1:], 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
)
Trying to access an uploded file like http://127.0.0.1:8000/media/videos/julian_06.flv, I get
ValueError at /media/videos/julian_06.flv
dictionary update sequence element #0 has length 40; 2 is required

I'd recommend trying to follow the docs for static hosting in development
if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^media/(?P<path>.*)$', 'serve'),
)
EDIT:
Your dictionary should have a : not , between the 'document_root' and settings.MEDIA_ROOT
urlpatterns = patterns('',
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)

Related

User uploaded images not serve after debug=false in django?

install whitenoise urls.py setting
urlpatterns += static(settings.STATIC_URL, document_root = STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = MEDIA_ROOT)
urlpatterns += [re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, }), ]
also configure static root and static url in settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_ROOT = BASE_DIR / '/static/images/'
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
In urls.py file:
add this:
from django.views.static import serve
and add those two urlpatterns:
re_path('media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})
re_path('static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT})

Django: updating file

I'm trying to upload a file but once it's uploaded I'm not able to open it since it shows an error saying that there's no an object with that Primary key. I think the error(s) should be in urls or in the path (MEDIA_ROOT) but can't find out what is wrong.
Any suggestion?
setings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
models.py
class Foo(models.Model):
file = models.FileField(upload_to="file_folder/", blank = True, null = True)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
--EDIT--
I'm using the Admin interface so there's no Views.py
And the traceback is not shown, only the following error:
ERROR
Request Method: GET
Request URL:http://localhost:8000/admin/db_personal/foo/3/media/file_folder/django.pdf/
foo object with primary key u'3/media/file_folder/django.pdf' does not exist.
MEDIA_URL needs to be an absolute path, like STATIC_URL:
MEDIA_URL = "/media/"

Django Photologue Gallery Uploads == BAD REQUEST

After installing Photologue following the official guide I did initially succeed in uploading a .zip gallery, however at one point the system started giving me a 400 BAD REQUEST error, whenever I attempt to upload a new .zip gallery. The .zip files do however make it to my myapp/media/photologue/temp directory, but are not available otherwise.
The project is within a virtualenv.
My settings file looks like
INSTALLED_APPS = (
...
'photologue',
'south',
...
)
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = ''
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
from photologue import PHOTOLOGUE_APP_DIR
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
PHOTOLOGUE_APP_DIR,
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
PHOTOLOGUE_PATH = 'myapp.utils.get_image_path'
Whitin myapp/utils.py I have,
# myapp/utils.py:
import os
def get_image_path(instance, filename):
return os.path.join('path', 'to', 'my', 'files', filename)
And I have myapp/urls.py with the recommended settings as per
#/myapp/urls.py
urlpatterns = patterns('',
...
url(r'^photologue/', include('photologue.urls')),
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + urlpatterns
Thanks for looking.
I have same behaviour but without getting any error. Django 1.6.2, photologue 2.7, pytz 2013

Serving static media directly from heroku with Django

Due to not being able to store TinyMCE js files on my S3 bucket due to origin problems i'm trying to get Heroku to serve them up.
Here's what I've attempted so far but no luck. The browser url looks good (http://www.mysite.com/media/js/tiny_mce/tiny_mce.js) but heroku doesn't serve them up and returns a 404.
Here's my code:
Settings.py
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
TINYMCE_JS_URL = MEDIA_URL + 'js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = MEDIA_ROOT + 'js/tiny_mce'
urls.py
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_URL}))
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
I can serve static assets directly from heroku using following code:
settings.py:
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
TINYMCE_JS_URL = MEDIA_URL + 'js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = MEDIA_ROOT + 'js/tiny_mce'
urls.py:
urlpatterns = patterns('',
...
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True, }),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True, }),
)
base.html:
<script type="text/javascript" src="{{ MEDIA_URL }}js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode: "textareas",
theme: "advanced",
forced_root_block: false,
force_p_newlines : false,
force_br_newlines : true,
});
</script>
Ok got it working using the comments in a github discussion https://github.com/aljosa/django-tinymce/pull/15
Primarily I changed the urls.py:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': '/app/.heroku/python/lib/python2.7/site-packages/tinymce/static/'}))
I've got a feeling this could be much better resolved but i'm out of ideas and this works
For Django >= 2.0.0, For serving MEDIA_URL directly from heroku you can use
from django.urls import include, path, re_path
from django.views.static import serve
urlpatterns = [
...
re_path(r'^media/(?P<path>.*)$', serve,
kwargs=dict(document_root=settings.MEDIA_ROOT)),
]
Remember, heroku removes MEDIA_ROOT folder with every deploy.
More info
https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

django static files weird behaviour

in my settings.py I have:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
# ... #
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
# ... #
STATIC_URL = '/static/'
in my urls.py I have:
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
and in my template:
<img src="{{ STATIC_URL }}images/myimage.png">
Which doesn't work. The weird thing is that if I change my settings.py:
STATIC_URL = 'static/'
and my template to:
<img src="/{{ STATIC_URL }}images/myimage.png">
does work!! I have been chasing round in circle to fix this, and I have looked at lots of forum posts, but I can't seem to figure out what I'm doing wrong.
if you use staticfiles app you don't need this (assuming you are using Django 1.3):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
})
Its handled by the app automatically. Try to remove and see what happens.