Django: Migrating from MEDIA_URL to STATIC_URL - django

Since Django 1.3 the concept of STATIC_URL has been introduced to separate use media files from css and js files.
I have set my STATIC_ROOT = '/home/user/project/static/' and
STATIC_URL = '/static/'.
In my base.html, i have changed the path like this:
<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">
And in url.py I have added the following two lines:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
My view renders the template with RequestCOntext:
return render(request, 'main_page.html', variables)
But in the development I still get 404 when running runserver.
[18/Aug/2012 17:12:04] "GET /static/jquery/jquery-1.8.0.min.js HTTP/1.1" 404 1682
What could I be missing?

settings.py
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'site_media')
MEDIA_URL = '/site_media/'
STATIC_URL = '/static/'
if DEBUG:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
else:
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^site_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}),
)
That should work :)

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})

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

Why is django not serving my media files?

In my settings.py,
ROOT_PATH = os.path.dirname(__file__)
#STATICFILES_DIRS =
STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
MEDIA_ROOT = os.path.join(ROOT_PATH, 'media')
STATIC_URL = '/static/'
MEDIA_URL = '/media/
in my template,
<div class=" event_image">
<img src="{{ MEDIA_URL }}{{ restaurant.logo }}" />
</div>
This doesnot work in development, it returns a 404 "GET /media/restaurant_detail/restaurant_detail/information_about_object.jpg HTTP/1.1" 404 178672
what am i doing wrong,what is the right way to go about it so it works in both in dev't and production. i looked here (Django 1.4 serving MEDIA_URL and STATIC_URL files on development server) but all in vain.
Turns out i had to include the urls,my urls
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
The above worked..
my project structure
-myproject
-- media
-- static
-- templates
-- settings.py
-- manage.py
-- app
Suziemac, It worked for me too. Here is a link for original documentation: https://docs.djangoproject.com/en/1.10/ref/views/ .
When I was developing an app I have used + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT), however this did not work when I deployed an app.

Django 1.4 serving MEDIA_URL and STATIC_URL files on development server

Just upgraded to Django 1.4, and having serious trouble with the new 'improved' serving of static and media files on development server. I love Django, but why on earth they have made serving these files doubly more complicated with STATIC_URL,STATIC_ROOT, STATICFILES_DIR now is utterly beyond me.
I'm simply trying to serve all files, static and uploaded, on the development server. I have the STATIC_URL files working, after much experimentation, but I simply cannot get the MEDIA_URL files to be served as well.
Settings:
DIRNAME = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(DIRNAME, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
I've got the media and static context processors added:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
'django.core.context_processors.media',
'django.core.context_processors.static',
"django.core.context_processors.request",
'satchmo_store.shop.context_processors.settings',
'django.contrib.messages.context_processors.messages',
)
and I've added in the url confs:
# serve static and uploaded files in DEV
urlpatterns += staticfiles_urlpatterns()
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
with the two conf settings added as indicated in the docs, first one for static, second for media.
my structure, website being an app and static dir placed inside it as instructed on djangoproject:
<myproject>
--media
--settings
--templates
--website
|->static
In templates I can serve static content no problem with
{{STATIC_URL}}css/style.css
But any uploaded image, this one using photologue, is not served, but the urls are correct:
/media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg
That directory structure does exit under media/
Super, super confused. It all seems so ridiculously complicated now, whereas I never had any issues before.
I´m very new to Django and I´ve never had any problem with static content.
This is my configuration. I hope it can help
My folder structure
django-project
--mainapp
----settings.py
----wsgi.py
----[...]
--otherapp
--fixtures
--static
--templates
--manage.py
--requirements.txt
settings.py
import os, socket
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MAIN_APP = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP, ".."))
MY_LOCALHOST = "VirusVault.local" # this is the real name of my local machine :)
try: HOST_NAME = socket.gethostname()
except: HOST_NAME = "localhost"
[...]
if HOST_NAME == MY_LOCALHOST:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = "/static/"
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media/')
MEDIA_URL = "/media/"
else:
STATIC_ROOT = "/server/path/to/static/files"
STATIC_URL = "http://server.com/static/"
MEDIA_ROOT = "/server/path/to/static/files/media/"
MEDIA_URL = 'http://server.com/static/media/'
You need to add 'django.contrib.staticfiles' to INSTALLED_APPS
urls.py
if settings.HOST_NAME == settings.MY_LOCALHOST:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
)
If you are using Django 1.4 folder structure, you've moved settings.py to your new website app folder which means your MEDIA_ROOT is now incorrect. Not sure if a relative location works in this case, but it should be something like this
MEDIA_ROOT = os.path.join(DIRNAME, '../media/')
It might be simpler to use an absolute path.
I intended to write a comment first but somehow add comment button doesn't work.
Did you check the permissions of media directory?
Since it became an answer, I am going to dump one of my perfectly working Django 1.4 sites configuration.
structure:
-myproject
-- media
-- static
-- templates
-- myproject
--- settings.py
--- urls.py
settings.py:
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) # Not the best way but works
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'
urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
...
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
urlpatterns += staticfiles_urlpatterns()

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.