Django 1.4 admin static files without staticfiles app - django

Django 1.4 release notes state:
If you're implicitly relying on the path of the admin static files
within Django's source code, you'll need to update that path. The
files were moved from django/contrib/admin/media/ to
django/contrib/admin/static/admin/.
Could somebody explain how this is done exactly? Up to Django 1.3 we used ADMIN_MEDIA_PREFIX in settings.py, which is now deprecated. However, since we are developing all the time on our static files (js, css, ...), the staticfiles app is a rather annoying nogo for us. Calling collectstatic after each modification is a nightmare :-P
A pure Python/Django solution would be great. If that's impossible, we are using LighTPD as server and not Apache.

manage.py collectstatic is used when you deploy, during development you can have django serve your static and media files by adding this to your url.py:
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
...
if settings.DEBUG:
# add one of these for every non-static root you want to serve
urlpatterns+= static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# this take cares of static media (i.e. bundled in apps, and specified in settings)
urlpatterns+= staticfiles_urlpatterns()
This will also serve all the static files that are bundled with reusable apps. This avoids the real nightmare of having to add symlinks to your webserver root for every third party app per project!

Oops, I just found the solution in Django's new documentation:
https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/#serving-the-admin-files

Related

How to serve Django static files during development without having to run "collectstatic"?

I have a Django version 2.2.6 application that has my Django static files being served up from a separate dedicated file server. I use the Django "collectstatic" command to update the static files on my file server whenever one of them changes. I also use the django-pipeline package so that each static file contains a special string in the file name. This prevents my users' browsers from loading a static file from cache if I've updated that file. This configuration works perfectly.
I'm now in a phase in which I'm making constant changes to my CSS file to create a new look and feel to my website and it's a pain to have to remember to run the collectstatic command after each little change I make. Is there a way I can temporarily "toggle" this collectstatic configuration off while I'm doing development so that I don't have to constantly run the collectstatic command? I seem to recall there was a way to change the main urls.py file and set DEBUG = True to do something like this back in Django 1.8 but I don't see it mentioned in the latest Django documentation. What is the current "best practice" for doing this?
I think it still is in the docs.
https://docs.djangoproject.com/en/2.2/howto/static-files/#serving-static-files-during-development
In urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Why is adding routes for static files recommended in development?

I have a little project using some static files in my applications and everything works fine.
But in development, it seems recommended to add something like this :
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In my urls.py.
But in development (DEBUG=True), runserver seems able to serve static files even if I don't run collectstatic. So I assume it serves it directly from the statics application's subdirectories.
So why is recommended to add a static() call to the urls.py file if we can do without it? Plus static() won't work with DEBUG=False anyway.
From the docs:
If you use django.contrib.staticfiles as explained above, runserver
will do this automatically when DEBUG is set to True. If you don’t
have django.contrib.staticfiles in INSTALLED_APPS, you can still
manually serve static files using the
django.contrib.staticfiles.views.serve() view.
The static() helper function adds an url for the serve() view to your urlpatterns.
As you can see, static files are only served automatically in the development server if DEBUG is set to True, and django.contrib.staticfiles is in your INSTALLED_APPS. If the latter is not the case, using the static() helper is the recommended way.

Identical and simple way of serving static files in development and production - is it possible?

Is it possible to have static files only in PROJECT_DIR/static directory without duplicates of static files in app dirs and without needing to do collectstatic command? On local computer Django dev server is used, in production - some other web server. From what I have read so far I decided that it's impossible, but maybe it's not true.
Of course it's possible.. the static files app is there to be useful. If you dont like "duplicates" (that's the whole point - you can have files per app, all merged into one area), don't use the staticfiles app.
Just use any folder, anywhere, as your assets folder. In production, serve it at some url, say MY_URL. In development, wire up your URLConf to serve files at your asset folder at MY_URL
https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static('MY_URL', document_root='path-to-my-files')
This is the old school method of doing this, before staticfiles brought its goodness.
Are you sure you can't solve this problem by just using the staticfiles app? It's not much work to add in a python manage.py collectstatic --noinput in your deployment script.

Django not serving some static files

I'm facing quite a strange issue with Django, my STATIC_URL is /static/ and I've got an app installed using pip as -e git+git://github.com/foo/bar.git#egg=bar which loads it into the virtualenv src folder.
Now, I'm trying to GET a static file related to this app as /static/bar/js/something.js and the src folder does have a bar/bar/static/bar/js/something.js file. Even if I findstatic bar/js/something.js it gives me a valid path to the file. However when I access it using the browser, I'm 301'd to /static/bar/js/something.js/ which 404s.
Why would this be happening?
Even poking around with staticfiles app, I was able to see that it recognizes my app as installed and that its static folder exists. However somehow the static files aren't being served.
Make sure that you have set up the django development server to actually server the static files for you.
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
Try using this urlpattern:
url( r'(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/file/'}),

serving static files on Django production tutorial

Does anyone have a simple step-by-step tutorial about serving static files on a Django production app? I read the Django docs and it sounds really complicated... I'm trying to go the route of serving static files using a different server like lighttpd, nginx, or cherokee, but setting these up is all Greek to me. I downloaded lighttpd, tried to follow the instructions to install, and within a few seconds got an error. Missing this or that or whatnot... I'm not a UNIX whiz and I'm not very good at C/C++, so all this ./configure and MAKE install are gibberish to me... So I guess my immediate questions are:
Which server would you recommend to serve static files that's easy to install and easy to maintain?
Assuming I actually get the server up and running, then what? How do I tell Django to look for the files on that other server?
Again, anyone has step-by-step tutorials?
Thanks a lot!
Sorry, don't have a step by step tutorial. But here is a high level overview that might help:
You probably want to go with the Apache server ( http://httpd.apache.org/) This comes with most *nix distributions.
You then want to use mod python (or as the commenter pointed out mod_wsgi: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/) to connect to Django : http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs. Once you complete this step, Apache is now fronting for Django.
Next you want to collect the static files in your Django into one directory and point apache at that directory. You can do this using the the ./manage.py collectstatic if you used django.contrib.staticfiles (http://docs.djangoproject.com/en/dev/howto/static-files/.)
So the trick is you're not telling Django to delegate serving static files to a specific server. Rather you're telling httpd which urls are served via Django and what urls are static files.
Another way of saying this is that all requests come to the Apache web server. The webserver, according to the rules you specify in httpd.conf, will decide whether the request is for a static file or whether it is for a dynamic file generated by django. If it for a static file it will simply serve the file. If the request is for a dynamic file it will, via modpython, pass the request to Django.
Hope that helps.
Development
STATICFILES_DIRS should have all static directories inside which all static files are resident.
STATIC_URL should be /static/ if your files are in local machine otherwise put the base URL here e.g. http://example.com/.
INSTALLED_APPS should include django.contrib.staticfiles.
In the template, load the staticfiles module:
{% load staticfiles %}
<img src='{% static "images/test.png" %}' alt='img' />
Production
Add STATIC_ROOT that is used by Django to collect all static files from STATICFILES_DIRS to it.
Collect static files:
$ python manage.py collectstatic
Add the path to urls.py:
from . import settings
urlpatterns = patterns('',
..
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)
More detailed articles are listed below:
http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated
http://agiliq.com/blog/2013/03/serving-static-files-in-django/
With the latest Django version like Django 3.2.6 I was having issues serving media and static files both in the dev and prod environment while DEBUG = False.
So I got around a solution that came from multiple stack overflow posts.
Import appropriate functions to urls.py
from django.urls import include, path, re_path
from django.views.static import serve
Define static URL pattern list to urls.py
static_urlpatterns = [
re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT}),
re_path(r"^static/(?P<path>.*)$", serve, {"document_root": settings.STATIC_ROOT}),
]
Assuming your STATIC_ROOT and MEDIA_ROOT is already defined in settings.py file
Just include static_urlpatterns in urlpatterns
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(api_urlpatterns)),
path("", include(static_urlpatterns)),
]
Hope it works for you both in the dev and prod environment when DEBUG = FALSE. Thank you.
Updated for urls.py
the url(....) format doesn't work anymore in urls.py for Django 3.0.7.
you need to do then:
urls.py:
from django.conf import settings # to import static in deployment
from django.conf.urls.static import static # to import static in deployment
....
urlpatterns = [
....
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # to import static in deployment
Reference: https://docs.djangoproject.com/en/3.0/howto/static-files/