Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install django-attachments

How to install django-attachments

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install django-attachments
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.3.1 Available View build log
Windows (64-bit)
0.3.1 Available View build log
Mac OS X (10.5+)
0.3.1 Available View build log
Linux (32-bit)
0.3.1 Available View build log
Linux (64-bit)
0.3.1 Available View build log
Depended by
Lastest release
version 0.3.1 on Jan 5th, 2011

django-attachments is a generic set of template tags to attach any kind of files to models.


1. Put attachments to your INSTALLED_APPS in your settings.py within your django project.

  1. Add (r'^attachments/', include('attachments.urls')), to your urls.py.

3. Add 'django.core.context_processors.request' to your TEMPLATE_CONTEXT_PROCESSORS in your settings.py. If this setting does not exist, simply add the following snippet at the end of your settings.py:

System Message: WARNING/2 (<string>, line 20)

Literal block expected; none found.

TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.auth', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.request', )

  1. Don't forget to resync your database:

    System Message: WARNING/2 (<string>, line 29)

    Literal block expected; none found.

./manage.py syncdb

  1. Grant the user some permissons:
  • For adding attachments grant the user (or group) the permission

System Message: WARNING/2 (<string>, line 34)

Bullet list ends without a blank line; unexpected unindent.


  • For deleting attachments grant the user (or group) the permission

System Message: WARNING/2 (<string>, line 37)

Bullet list ends without a blank line; unexpected unindent.

attachments.delete_attachments. This allows the user to delete only attachments which are assigned to him (rather the attachments he uploaded self).

  • For deleting foreign attachments (attachments by other users) grant

System Message: WARNING/2 (<string>, line 41)

Bullet list ends without a blank line; unexpected unindent.

the user the permission attachments.delete_foreign_attachments.

This only works for the templatetags, the admin still allows anybody to add or delete attachments.

Mind that you serve files!

django-attachments stores the files in your site_media directory and does not modify them. For example, if an user uploads a .html file your webserver will probably display it in HTML. It's a good idea to serve such files as plain text. In a Apache2 configuration this would look like:

<Location /site_media/attachments>

System Message: ERROR/3 (<string>, line 56)

Inconsistent literal block quoting.

AddType text/plain .html .htm .shtml .php .php5 .php4 .pl .cgi </Location>


In contrib.admin:

django-attachments provides a inline object to add a list of attachments to any kind of model in your admin app.

Simply add AttachmentInlines to the admin options of your model. Example:

System Message: WARNING/2 (<string>, line 71)

Literal block expected; none found.

from django.contrib import admin from attachments.admin import AttachmentInlines

class MyEntryOptions(admin.ModelAdmin) inlines = [AttachmentInlines]

In your frontend templates:

First of all, load the attachments_tags in every template you want to use it:

{% load attachments_tags %}

django-attachments comes with some templatetags to add or delete attachments for your model objects in your frontend.

1. get_attachments_for [object]: Fetches the attachments for the given model instance. You can optionally define a variable name in which the attachment list is stored in the template context. The default context variable name is attachments Example:

{% get_attachments_for entry as "attachments_list" %}

2. attachment_form: Renders a upload form to add attachments for the given model instance. Example:

{% attachment_form [object] %}

It returns an empty string if the current user is not logged in.

3. attachment_delete_link: Renders a link to the delete view for the given attachment. Example:

{% for att in attachment_list %}
{{ att }} {% attachment_delete_link att %}
{% endfor %}

This tag automatically checks for permission. It returns only a html link if the give n attachment's creator is the current logged in user or the user has the delete_foreign_attachments permission.

Quick Example:

{% load attachments_tags %}
{% get_attachments_for entry as "my_entry_attachments" %}

{% if my_entry_attachments %} <ul> {% for attachment in my_entry_attachments %} <li> <a href="{{ attachment.attachment_file.url }}">{{ attachment.filename }}</a> {% attachment_delete_link attachment %} </li> {% endfor %} </ul> {% endif %}

{% attachment_form entry %}


v0.3.1 (2009-07-29):

  • Added a note to the README that you should secure your static files.

v0.3 (2009-07-22):

  • This version adds more granular control about user permissons. You need

System Message: WARNING/2 (<string>, line 145)

Bullet list ends without a blank line; unexpected unindent.

to explicitly add permissions to users who should been able to upload, delete or delete foreign attachments.

This might be backwards incompatible as you did not need to assign add/delete permissions before!

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:3

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.