I have read Django {{ MEDIA_URL }} blank and How do I include image files in Django templates? questions but error still here: MEDIA_URL is empty in template.
My code:
settings.py:
MEDIA_ROOT = "D:/blizzard/Projects/Python/Web/moz455/app/media" # or os.path.join(SiteDir, "app/media")
MEDIA_URL = '/media/'
STATIC_ROOT = 'F:/Soft/Python26/Lib/site-packages/django/contrib/admin/static'
STATIC_URL = '/static/'
views.py:
def gallery(request):
ResponseDict = {}
return render_to_response('gallery.html', ResponseDict,
context_instance = RequestContext(request))
urls.py:
urlpatterns = patterns('',
(r'^gallery/', gallery),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
) + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Make sure you have included django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.media",
...
)
Related
I have problem with image in django. I get 404.
Settings.py
MEDIA_ROOT = 'static/image/'
MEDIA_URL = 'image/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Models.py
img = models.ImageField(upload_to = '', verbose_name= 'Картинка',null=True )
(P.S I tried everything,but it isnt working)
Have you got your answer or not if not try this
Add this in your settings.py file
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #new
MEDIA_URL = '/media/' #new
After Add in your main urls.py file add these config
from django.conf import settings #new
from django.conf.urls.static import static #new
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) #media
I have a class Media as follows
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['choice.js']
admin.site.register(hello,helloAdmin)
The staticfiles app prepends STATIC_URL to the media path. But I want MEDIA_URL to be prepended instead of STATIC_URL and STATIC_URL isn't empty. How can this be done?
You can achieve that with the following settings:
import os
CURRENT_PATH = os.path.abspath((os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(CURRENT_PATH, 'uploads')
MEDIA_URL = '/site_media/'
urls.py
from django.conf import settings
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
admin.py
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['/site_media/choice.js']
admin.site.register(hello,helloAdmin)
Using MEDIA_URL is not advised for serving static files. According to django documentation, MEDIA_URL will not be referred for media files, when STATIC_URL is not empty. Refer to this part of documentation.
So I recommend you to use the following settings:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(CURRENT_PATH, 'media'),
)
admin.py
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['choice.js']
admin.site.register(hello,helloAdmin)
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 :)
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.
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}),
)