Snippet List
This allows the mod_xsendfile module for Apache safely serving private files. Django take cake about processing and permissions checking, Apache server requested files.
Installation of mod_xsendfile:
$ tar -xzvf mod_xsendfile-0.12.tar.gz
$ /usr/sbin/apxs -c mod_xsendfile-0.12/mod_xsendfile.c
$ ld -Bshareable -o mod_xsendfile-0.12/mod_xsendfile.so mod_xsendfile-0.12/mod_xsendfile.o
Copy mod_xsendfile.so to your local Apache modules folder.
Modify httpd.conf to load an enable the module:
LoadModule xsendfile_module modules/mod_xsendfile.so
Add to virtual host container:
<Virtual ...:80>
XSendFile On
XSendFilePath /home/django_projects/mysite/media/
</Virtual>
- http
- media
- static
- xsendfile
Create in your template dir html files named example.static.html and with this snippet you can get the static page with the url /example/. If you put static file in a sub-directory, the url will be /sub-directory/example/
**Example:**
`static_urls = StaticUrls()`
`urlpatterns = patterns('', *static_urls.discover())`
`urlpatterns += patterns('',`
`(r'^admin/doc/', include('django.contrib.admindocs.urls')),`
`(r'^admin/', include(admin.site.urls)),`
`)`
- urls
- static
- static files
- url pattern
This view has the same functionality as Django's builtin static view function but when the configuration option `USE_XSENDFILE = True` is specified in the settings it returns the absolute path of the file instead of its contents.
This allows the mod_xsendfile module in Apache or Lighttpd to take care of efficiently serving the file while still allowing for Django to take care of other processing of the request, like access control or counting the amount of downloads.
Serves images from a local directory, but if it doesn't find it locally, then go look for it on a server. This is useful for sites with very large amounts of static content.
Instead of copying all your prod images to dev every time you update the database, simply use this in your URL patterns to serve the images:
`(r'^(?P<path>.*)$', 'static_fallback.serve', {'document_root' : '/path/to/my/files/'})`
By default, it caches the images locally in the path it was supposed to find them. The fallback server URL can be specified in urls.py or settings as FALLBACK_STATIC_URL.
Thanks to Johnny Dobbins for the idea. Based on the Nginx pass through image proxy concept.
For more info [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
- image
- static
- images
- fallback
If you request static files such as images, javascript or css using http rather than https, the browser will complain that your site is not secure.
This simple context processor will replace http:// with https:// in your MEDIA_URL if your static files are being included from an https page.
In your settings.py just replace 'django.core.context_processors.media' with your new context processor.
- files
- ssl
- context
- static
- processor
- secure
- content
- media_url
I was in need to have pluggable components that all have more or less some media files. I didn't want to pollute my config with dozens of media paths so I wrote this custom command that copies contents `<appdir>/app_media` to your `MEDIA_ROOT/<appname>` path.
In template you will refer your media files like `{{MEDIA_URL}}/appname/<path to media>`
- media
- static
- custom
- command
Default to a static template.
**Example:**
urlpatterns = patterns('',
...
# this rule SHOULD BE the last one
(r'^(?P<template>[a-z-_/]+/?)?$',
'myproj.apps.myapp.views.static_template'),
)
This handler is useful if you serve static/media files directly from Apache only to authenticated users. It checks if a user is not anonymous (and therefore logged in) and redirects to the login site if needed.
The following apache config activates the handler:
<Location "/site_media/company1">
#SetHandler None ##Uncomment if you serve static files in the same virtual host
PythonOption DJANGO_SETTINGS_MODULE mysite.settings
PythonAuthenHandler mysite.myhandler
PythonPath "['/path/to/project'] + sys.path"
Require valid-user
</Location>
- apache
- mod_python
- auth
- static
This module is comes from the original staticview.py from django. But when you using it, it can looks for static files in your app's subdirectory. So that you can put static files which concerned with your app in a subdirectory of the app. I should say it method only suit for developing, so if you want to deploy your application to apache, you should copy static folder to the real media folder. And if you keep the same structure of the directory, then it'll be very easy. And you can write a little script to automatically do that. But for now, I didn't have written one yet :P
How to use it in urls.py
--------------------------
Here's an example:
(r'^site_media/(.*)$', 'utils.staticview.serve', {'document_root': settings.SITE_MEDIA, 'app_media_folder':'media'}),
It seems just like the original one in django. But there is a new parameter "app_media_folder", it's used for subdirectory name of app. So your django project folder structure maybe seem like this:
/yourproject
/apps
/appone
/media
/css
/img
/js
/media
/css
/img
/js
12 snippets posted so far.