I'm using the staticfiles app during development, which doesn't work unless DEBUG is turned on.
From the docs:
Warning This will only work if DEBUG is True.
That's because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
Additionally, when using staticfiles_urlpatterns your STATIC_URL
setting can't be empty or a full URL, such as
http://static.example.com/.
I'm trying to view my Http404 templates, though, and of course they don't work in DEBUG mode. So I'm in a catch 22 - if I want to view the 404 page I have to turn off DEBUG, but then no static files are servers and I can't see any images, etc.
You can simply pretend you are in production. Run:
python manage.py collectstatic --noinput
To have all your files copied to STATIC_ROOT. Then, temporarily add the following to urls.py:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
You'll have to run collectstatic each time you make a change to any static files, so I would suggest live-editing in something like Firebug and then saving the finished product. Also, remember to delete the static directory and remove that line from urls.py when you're done.
I haven't tried it myself, but you might try setting DEBUG_PROPAGATE_EXCEPTIONS = True
https://docs.djangoproject.com/en/dev/ref/settings/
Related
My Django project is displaying media in templates if debug == True no problem at all but the problem arises when I set debug to False
Django fails to load them if debug == False( There are some solutions out there but I am not able to wrap my head around them as
I am new to Django ). Can someone please tell me what is the best and easiest way to solve this issue?
Note: I want to host my website on my local wifi (not on pythonanywhere or any other web hosting service)
Thanks in advance.
For me below method worked:
In urls.py I added this line:
from django.views.static import serve
add those two urls in urlpatterns:
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
and both static and media files were accesible when DEBUG=FALSE.
Hope it helps
For more info. , Take a look at Why does DEBUG=False setting make my django Static Files Access fail?
This is my folder structure
Music
-Music
-Feature
-static
-feature
core.css
-css
other css files
-js
-img
-templates
404.html
500.html
index.html
-feature
about.html
detail.html
template.html
manage.py
views.py
from django.shortcuts import render
def error404(request):
return render(request,'404.html')
urls.py
urlpatterns = [
url(r'^featured/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^about/$', views.about, name='about'),
url(r'^FAQ/$', views.faq, name='faq'),
]
handler404 = 'mysite.views.error404'
The custom 404.html file gets rendered but with no css.And normally the css works fine on other pages but when I set debug=falseto check the custom 404 error page in settings.py the css for the entire project disappears. Something to do with folder structure or some other problem?
edit: core.css is the main css file and the part with other css files contains css for plugins
It's about serving static files. When you use DEBUG = True then django takes care about them otherwise your server should do it. Django in debug mode uses this view. The warning from there:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
You can run your server with --insecure option just to test 404 error or you can explicilty create url for that page to check its styling:
Use the --insecure option to force serving of static files with the
staticfiles app even if the DEBUG setting is False. By using this you
acknowledge the fact that it’s grossly inefficient and probably
insecure. This is only intended for local development, should never be
used in production and is only available if the staticfiles app is in
your project’s INSTALLED_APPS setting. runserver --insecure doesn’t
work with CachedStaticFilesStorage.
Your handler404 and view are okay. But you don't need them. Just a custom 404 template is enough. See: https://docs.djangoproject.com/en/1.8/topics/http/views/#the-http404-exception
In order to use the Http404 exception to its fullest, you should
create a template that is displayed when a 404 error is raised. This
template should be called 404.html and located in the top level of
your template tree.
The template is in the right location. I think the problem is serving your static files. Open developer tools in your browser to see what resources fail to load (console, network or sources tab). Inspect the paths. Is there an external style sheet link in the head section of the 404 source? (elements tab or view source code).
https://docs.djangoproject.com/en/1.8/howto/static-files/
As stated in topic, my Django site media url is returning 404 after trying to access it. Everything was working flawless until I wanted to end the development process and set
DEBUG = True
in settings.py to have the site finished once and for all. When I change DEBUG back to
DEBUG = False
it works fine once again. I have no idea what's the problem, any suggestions?
This is by design: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-during-development
If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True.
That being said, you can use the following workaround by modifying your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Note that this is highly inefficient and not encouraged for production use. You should normally configure your web server (apache, nginx, etc) to serve your static and media content.
Community,
I am using Django 1.7. Due to specific nature of the project, I have complicated needs for handling static files. In other words, I need /static/ to be served on development just as usual, but /static/blueprints/ subdirectory should be served by a custom view.
It seems anyway that static serving view has priority above everything in urls.py. The following just does not work:
urlpatterns = patterns('',
url(r'^static/blueprints/(?P<blueprint>[\w-]+)/(?P<path>.+)', 'my_view_name'),
...
)
The view never gets fired when accessing an appropriate URL. But changing static/blueprints to, say, my_static/blueprints makes this path to work, so the view actually does work.
Of course, I need that only on development; on production, a script will be used to form the necessary directory structure served by Nginx. So solutions:
Avoid using debug=True on devserver. But Django's debugger is very useful.
Avoid using django.contrib.staticfiles. Not pleasing too, I'd like to use the collectstaic command.
Use just /blueprints/ path instead of /static/blueprints/, which is a solution, but just against aesthetics.
Is there any more elegant solution?
You're using ./manage.py runserver on your dev server, right? Then you can go this way: ./manage.py runserver --nostatic, and in urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url(r'^static/blueprints/(?P<blueprint>[\w-]+)/(?P<path>.+)', 'my_view_name'),
...
)
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
When I was using the built-in simple server, everything is OK, the admin interface is beautiful:
python manage.py runserver
However, when I try to serve my application using a wsgi server with django.core.handlers.wsgi.WSGIHandler, Django seems to forget where the admin media files is, and the admin page is not styled at all:
gunicorn_django
How did this happen?
When I look into the source code of Django, I find out the reason.
Somewhere in the django.core.management.commands.runserver module, a WSGIHandler object is
wrapped inside an AdminMediaHandler.
According to the document, AdminMediaHandler is a
WSGI middleware that intercepts calls
to the admin media directory, as
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
Use this ONLY LOCALLY, for development! This hasn't been tested
for
security and is not super efficient.
And that's why the admin media files can only be found automatically when I was using the test server.
Now I just go ahead and set up the admin media url mapping manually :)
Django by default doesn't serve the media files since it usually is better to serve these static files on another server (for performance etc.). So, when deploying your application you have to make sure you setup another server (or virtual server) which serves the media (including the admin media). You can find the admin media in django/contrib/admin/media. You should setup your MEDIA_URL and ADMIN_MEDIA_URL so that they point to the media files. See also http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files.
I've run into this problem too (because I do some development against gunicorn), and here's how to remove the admin-media magic and serve admin media like any other media through urls.py:
import os
import django
...
admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^' + admin_media_url , 'django.views.static.serve', {
'document_root': admin_media_path,
}, name='admin-media'),
...
)
Also: http://djangosnippets.org/snippets/2547/
And, of course, #include <production_disclaimer.h>.