Django not serving media files if I check for settings.DEBUG - django

This serves the media files correctly:
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This doesn't serve the media files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ ... ]
if settings.DEBUG:
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The documentation writes "This is not suitable for production use!", so that is why I need a way to check for DEBUG before serving media files. How can I do that. Why does this approach doesn't work?

Use
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
(notice '=' after '+' - in your version you are adding static() patterns but not assigning the result to the urlpatterns variable)

Related

Django medias(pictures) suddenly not loading

I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading.
My media path in Settings.py :
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
I have added this to the end of my urls.py of the app:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and as i said, it was working fine for a long, suddenly this happened
edit: I just figured it out that this is happening when I am using redirect function in one of my views
Another way to do it is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# other URLs ...
]
# If DEBUG=True in the settings file
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So we're adding static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to the urlpatterns if DEBUG=True in the settings.py file. This gives a better management in some sense.
I had the same issue and this helps me :
Add this in your urls.py in the urlpatterns list :
from django.views.static import serve
from django.conf import settings
from django.conf.urls import url
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
# YOUR URLS ARE HERE
]
Of course stay in Debug=True in settings.py.

not able to access uploaded media files in django

This is the error i am getting
template does not exist
This is my settings file I have added media root and media directory in settings.py
These are my urls urls
add this to your urls.py file
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django debug toolbar 404 with wagtail

I am trying to get django-debug-toolbar working. I have followed the installation steps and I get the sidebar including stats (e.g. SQL 1 query in 2.75ms, Static Files 19 Files used, 30 receivers of 12 signals) which seem to be legitimate and indicate that its working.
However, when I click for more info on a given tab, I get a 404 in browser, and this sort of thing in the console:
"GET /__debug__/render_panel/?store_id=ac74875cfe864b2dab4c6d17c1d1ed5d&panel_id=RequestPanel HTTP/1.1" 404 1791"
Other pages on site do work.
I have tried various configurations in urls.py. Here is what I currently have:
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r'^pages/', include(wagtail_urls)),
]
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
# urlpatterns = [
# url(r'^__debug__/', include(debug_toolbar.urls)),
# ]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have tried a few other configurations here, including having:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
in the if settings.DEBUG: block.
Any ideas?
As the comment says, the wagtail urls must be the last pattern.
One option would be to move the debug toolbar urls to the beginning of the list:
urlpatterns = [
...
]
if settings.DEBUG:
urlpatterns = [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
Or you could remove the wagtail urls from there current position, and add them after your if settings.DEBUG: block.
if settings.DEBUG:
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += [
url(r'', include(wagtail_urls)),
]
I’ve included the debug toolbar urls inside and if settings.DEBUG: block here because that’s what the docs recommend, but that’s not the reason why it works. The key is to make sure that the wagtail urls come at the very end.

Storing images in Django model and media folder

I have been following this post
However I do not understand this part in which the answer states
In url.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [....
]
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now
In my project if I add
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
after my
urlpatterns = [....
]
I get the error
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
TypeError: bad operand type for unary +: 'list'
any suggestions on how i can fix this ?
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Serving static files in development mode

I've read the documentation of the Django about serving static files, and it says you should use django.conf.urls.static.static function in order to server them.
Here is my code:
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from settings import STATIC_ROOT, STATIC_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/v1/', include('news.rest_urls', namespace='rest_framework')),
static(STATIC_URL, STATIC_ROOT)
]
And this is the error I receive:
'list' object has no attribute 'regex'
You need to define your STATIC_URL and STATIC_ROOT in settings.py and add to your urlpatterns next:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)