How to override django static file development server? - django

I am beginning to use the application 'django.contrib.staticfiles' to collect static files into the /static/ directory of my project.
The problem is that, when I am using the django development server (manage.py runserver) it automatically serve static files.
It is usually fine, but in my case, I would like to serve these static files myself.
I would like to put in the urls.py file something like that :
urlpatterns += patterns('',
url('^static/(?P<path>.*)$', myStaticMediaServe,{'document_root': settings.STATIC_ROOT ,'show_indexes': True}),
)
The problem is that 'django.contrib.staticfiles' application has got the priority on '/static/' url when settings.DEBUG=True : I cannot find a way to make Django use my '/static/' urlpattern description while being in debug mode
If I remove 'django.contrib.staticfiles' from settings.py : my '/static/' urlpattern works but I loose static files collecting.
Do you have an idea to use 'django.contrib.staticfiles' AND use my own static files server through an urlpattern description AND have settins.DEBUG=True

I found that, by default, django 'runserver' itself, preempts /static/ urls : even with custom middleware, you cannot force django to point '/static/' to your code.
The only solution I found : use --nostatic option for './manage.py runserver', then one can use his own url patterns and views in order to serve static files.

Set DEBUG to False. Django only serves static files when it's True.

Related

Using static folding inside app folding Django

I have a static directory inside my apps folder. I added this line of code to my projects settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
The file continues to result in a 404 not found. I'm getting stuck, what should I do?
It probably isn't mistake in your static files settings. But to be sure the relevant settings for static folder in app directories is STATICFILES_FINDERS https://docs.djangoproject.com/en/2.1/ref/settings/#staticfiles-finders. But the AppDirectoriesFinder should be present by default so it is probably OK.
Your problem is probably in your URL scheme:
For development purposes make sure you have django.contrib.staticfiles in your installed apps, DEBUG = True and STATIC_URL = '/static/' (or something similar). Then you should be able access the files.
For deploying it is more complicated but essentially you have setup STATIC_ROOT, before starting the server run python manage.py collectstatic command which will copy all static files to one location and then setup your server (Nginx) to serve the files from this folder under /static/ URL. Here is the official documentation https://docs.djangoproject.com/en/2.1/howto/static-files/#deployment
Note: if your app is foo and you have bar file in your static directory under foo app the URL for the file will be /static/foo/bar.

Using Django static() to generate audio files' URLs in production

I'm trying to get my Django app to play audio files (supposedly) uploaded by users via a form. Said files are tied to a model :
# models.py
class Doc(models.Model):
# ...
tape = models.FileField()
The uploading and saving parts are working fine, and the files are stored where they should be :
- djangoproject
|
- docapp
|
- media <- here
So, in order to get where I want, I added these two lines to the settings.py file MEDIA_ROOT = os.path.join(BASE_DIR, 'docapp/media/') and MEDIA_URL = 'docapp/media/'.
I hoped to be able to link to the audio files thus:
# templates/docapp/index.html
...
<audio src='{{ doc.tape.url }}' controls></audio>
Unfortunately, this wasn't working because the link generated by doc.tap.url (http://localhost/docapp/media/filename.aac) was returning a 404 error.
After a lot of googling I found this answer, which I happily copy-pasted into my app ... and it worked. This is the code in question :
# urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The problem is that I'm not comfortable with inserting code in my apps that I don't understand. I did some research about the static() function and all I could get is this :
Helper function to return a URL pattern for serving files in debug mode
Does this mean that the static function should not be used in production? If so, what should I be using in production? And what exactly does it do?
EDIT To be clear, the generated URL after injecting the solution is the same as the one generated without it. Yet, it only works when the static() function is present.
EDIT 2 I forgot to mention that the 404 errors persisted even after I chmoded the media folder to allow others to read-access it.
Thanks in advance!
You shouldn't do anything. No errors no problem. The docs write about development server and serving static files AND that it is for development only. In a production environment you configure your server (Apache, NGNIX or third party like S3) to serve the files. That's all.
Try to configure media files and access the file via it's url. If it works, try the {{ doc.tape.url }} template tag.
In your development environment your media may live in /media/ (route and directory). While on production it may be something like media.example.com. Running Django with the settings for that environment will change all static/media domains and paths to their correct locations.
You may split settings file into a settings file for each environment (production, acceptance, development). Like this:
project/
settings/
__init__.py
base.py
local.py
staging.py
test.py
production.py
You can run your project with a specific env: ./manage.py runserver --settings=project.settings.development. Do not repeat yourself and put development specific settings in development.py and from base import * so that base.py contains the default settings.
This project and settings layout is taken from the book Two Scoops of Django. It is just an example. Adjust to your own needs.
Yes, django.conf.urls.static.static is only for development and not for production. For production, you should just need to configure your MEDIA_URL and MEDIA_ROOT settings and have your webserver serve the MEDIA_ROOT directory in the MEDIA_URL path.
Basically adding that in the URL urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) will make the media files in the URL existing. Try removing that and visit your media file in the URL and you will receive a 404 not found. It's very similar to the concept that you are inserting a view for rendering the media files

Setting up django static directory for testing/development

In production, the apache server can handle serving static files just fine. For testing on my local machine I'm using manage.py runserver.
It seems the django apps I have somehow inject some default search paths into this debug server. The browser url shows everything under /static/whatever, but those files are coming from all over the place. This is fine for testing but when moving to production I'll need them in the one path. Thankfully, manage.py collectstatic provides a handy method to copy everything to the one directory given by STATIC_ROOT in settings.py. I've set mine to os.path.join(BASE_DIR, 'static/') where BASE_DIR is where manage.py is.
Now I want to start adding my own stuff in static, but everything's still being hosted by these hidden search directories provided by apps under DEBUG=True. I've tried the following in urls.py:
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
and
import re
from django.conf import settings
if settings.DEBUG:
static_url = re.escape(settings.STATIC_URL.lstrip('/'))
urlpatterns += patterns('',
(r'^%s(?P<path>.*)$' % static_url, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
Neither have worked.
I tried providing my static directory in STATICFILES_DIRS, but get the error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. So then I tried removing my STATIC_ROOT so the path was only given in STATICFILES_DIRS. Finally I can access my files in development (still with staticfiles_urlpatterns from above).
However, I'm pretty sure removing STATIC_ROOT is not the right way to go about it. For a start, I won't be able to run collectstatic after installing other apps without manually changing everything around again. How should I be configuring my static directory for development?
Also, is there a way to turn off hosting app specific static directories so I can make sure my single static is set up correctly (but still be in development mode)?
You're making this more complex than it needs to be. Include "static" in STATICFILES_DIRS as you are doing, for the project-level assets, but make STATIC_ROOT - ie the destination of the collectstatic command - something else, probably "staticfiles", and alias that directory to /static/ in Apache. Django will serve your assets from STATICFILES_DIRS automatically, without you even needing to add the urlpatterns.
OK, I completely missed the point here.
STATIC_ROOT is for collectstatic to pool all the static content for you. You don't add your files to here because it gets overwritten.
Now for production you can get your webserver to host the directory STATIC_ROOT and point STATIC_URL to the url it can be found through the webserver.
STATICFILES_DIRS allows you to add your own directories for collectstatic to search and pool to STATIC_ROOT. Then you can nicely separate static stuff for each of your apps and/or a common project static.
For dev, django expects everything to be in STATICFILES_DIRS and hosts that, not STATIC_ROOT because then there would be duplicates. This makes sense for larger projects because you want to be working on the per-app files, not some blob of stuff for a website.

Django Static Files Development

This seems to be a source of much confusion judging by the amount of similar titles on the subject, however trying everything I could find on static files with the django development server I've almost given up hope!
So my static files are served from C:/Users/Dan/seminarWebsite/static/, in which I have sub folders for images, css etc.
SETTINGS:
STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'
STATIC_URL = '/static/'
The static files app is also active.
URLS:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
TEMPLATE:
"{{ STATIC_URL }}images/vision.jpeg"
However only a broken link appears and at this address: http://127.0.0.1:8000/homepage/images/vision.jpeg and I don't think it should be at that address (homepage is the url name of the page the static image file is being called to).
Based on what you've posted so far, it looks like you're following the docs for django.contrib.staticfiles. I agree that the docs can be difficult to follow especially if one is new to django.
I believe the confusion stems from the fact that django.contrib.staticfiles has two modes of operation:
During the development phase where the development server is used, it dynamically searches for static files in predefined directories and make it available on STATIC_URL
For deployment, it assists in collating static files to a single directory (defined using STATIC_ROOT) so that the static files can be hosted using a webserver suited for static files. This collation is done using python ./manage.py collectstatic.
Here's a quick summary of how to get up and running. I haven't had a chance to try it out so there may be mistakes. Hopefully this will help you get started and at the very least help you understand the docs. When in doubt, do refer to the docs.
Hosting static files on the development server
Make sure you have 'django.contrib.staticfiles' in INSTALLED_APPS
Specify STATIC_URL. This will be the path where your static files will be hosted on.
STATIC_URL = '/static/'
Make sure your files are in the correct directories. By default, staticfiles will look for files within the static/ directory of each installed app, as well as in directories defined in STATICFILES_DIRS. (This behaviour depends on backends listed in STATICFILES_FINDERS).
In your case, you'd probably want to specify your directory in STATICFILES_DIRS:
STATICFILES_DIRS = (
'C:/Users/Dan/seminarWebsite/static/',
)
Make the view accessible by adding the following to the end of urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Make sure you have DEBUG = True in settings.py.
That's it.
If you run your dev server (./manage.py runserver), you should be able to access your file via http://localhost:8000/static/images/vision.jpeg (which serves C:/Users/Dan/seminarWebsite/static/images/vision/jpeg).
Linking to static files in your templates
There are two ways to get a correct link for your static files - using the staticfiles template tag, and making STATIC_URL accessible to your templates. Since you've attempted the latter, we'll stick to that.
Make sure you have 'django.core.context_processors.static' in TEMPLATE_CONTEXT_PROCESSORS. If you haven't redefined TEMPLATE_CONTEXT_PROCESSORS then there is nothing to do since it should be there by default.
Make sure you use RequestContext when rendering your template. Example:
from django.template import RequestContext
# ...
def some_view(request):
# ...
return render_to_response('my_template.html', {
"foo" : "bar", # other context
}, context_instance = RequestContext(request))
You should now be able to use the following in your my_template.html:
<a href="{{ STATIC_URL }}images/vision.jpeg" />
Hosting static files on production server.
If all the static files you need to serve are store in that one directory (C:/Users/Dan/seminarWebsite/static), then you're almost there. Simple configure your webserver to host that directory on /static/ (or whatever you set STATIC_URL to) and you're good to go.
If you have files scattered in different directories and/or app specific static files, then you'll need to collate them.
Set STATIC_ROOT to the directory where you want to store the collated files.
Run ./manage.py collectstatic to do the collation.
Configure your webserver to host the that directory (STATIC_ROOT) on /static/ (or whatever you set STATIC_URL to).

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/