diff -r 4dd0391a8f6b django/conf/global_settings.py
a
|
b
|
|
265 | 265 | # Example: "http://media.lawrence.com" |
266 | 266 | MEDIA_URL = '' |
267 | 267 | |
| 268 | # Absolute path to the directory that holds user uploaded media. |
| 269 | # If None, this will default to MEDIA_ROOT |
| 270 | USER_MEDIA_ROOT = None |
| 271 | |
| 272 | # URL that handles the media serverd from USER_MEDIA_ROOT |
| 273 | # If None, this will default to MEDIA_URL |
| 274 | USER_MEDIA_URL = None |
| 275 | |
268 | 276 | # List of upload handler classes to be applied in order. |
269 | 277 | FILE_UPLOAD_HANDLERS = ( |
270 | 278 | 'django.core.files.uploadhandler.MemoryFileUploadHandler', |
diff -r 4dd0391a8f6b django/conf/project_template/settings.py
a
|
b
|
|
43 | 43 | # calendars according to the current locale |
44 | 44 | USE_L10N = True |
45 | 45 | |
46 | | # Absolute path to the directory that holds media. |
47 | | # Example: "/home/media/media.lawrence.com/" |
48 | | MEDIA_ROOT = '' |
49 | | |
50 | 46 | # URL that handles the media served from MEDIA_ROOT. Make sure to use a |
51 | 47 | # trailing slash if there is a path component (optional in other cases). |
52 | 48 | # Examples: "http://media.lawrence.com", "http://example.com/media/" |
53 | 49 | MEDIA_URL = '' |
54 | 50 | |
| 51 | # Absolute path to the directory that holds user uploaded media. |
| 52 | # Example: "/home/media/media.lawrence.com/" |
| 53 | USER_MEDIA_ROOT = '' |
| 54 | |
| 55 | # URL that handles the media served from USER_MEDIA_ROOT. Make sure to use a |
| 56 | # trailing slash if there is a path component (optional in other cases). |
| 57 | # Examples: "http://media.lawrence.com", "http://example.com/media/" |
| 58 | USER_MEDIA_URL = '' |
| 59 | |
55 | 60 | # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a |
56 | 61 | # trailing slash. |
57 | 62 | # Examples: "http://foo.com/media/", "/media/". |
diff -r 4dd0391a8f6b django/core/context_processors.py
a
|
b
|
|
10 | 10 | from django.conf import settings |
11 | 11 | from django.middleware.csrf import get_token |
12 | 12 | from django.utils.functional import lazy |
| 13 | from django.utils.media import get_user_media_url |
13 | 14 | |
14 | 15 | def auth(request): |
15 | 16 | """ |
diff -r 4dd0391a8f6b django/core/files/storage.py
a
|
b
|
|
10 | 10 | from django.utils.encoding import force_unicode |
11 | 11 | from django.utils.functional import LazyObject |
12 | 12 | from django.utils.importlib import import_module |
| 13 | from django.utils.media import get_user_media_url, get_user_media_root |
13 | 14 | from django.utils.text import get_valid_filename |
14 | 15 | from django.utils._os import safe_join |
15 | 16 | |
… |
… |
|
127 | 128 | |
128 | 129 | def __init__(self, location=None, base_url=None): |
129 | 130 | if location is None: |
130 | | location = settings.MEDIA_ROOT |
| 131 | location = get_user_media_root() |
131 | 132 | if base_url is None: |
132 | | base_url = settings.MEDIA_URL |
| 133 | base_url = get_user_media_url() |
133 | 134 | self.location = os.path.abspath(location) |
134 | 135 | self.base_url = base_url |
135 | 136 | |
diff -r 4dd0391a8f6b django/utils/media.py
-
|
+
|
|
| 1 | |
| 2 | def get_user_media_url(): |
| 3 | from django.conf import settings |
| 4 | url = settings.USER_MEDIA_URL |
| 5 | if url is None: |
| 6 | import warnings |
| 7 | warnings.warn( |
| 8 | 'Please define the USER_MEDIA_URL setting (falling back to MEDIA_URL for now).', |
| 9 | PendingDeprecationWarning |
| 10 | ) |
| 11 | return settings.MEDIA_ROOT |
| 12 | return settings.MEDIA_URL |
| 13 | return url |
| 14 | |
| 15 | def get_user_media_root(): |
| 16 | from django.conf import settings |
| 17 | root = settings.USER_MEDIA_ROOT |
| 18 | if root is None: |
| 19 | import warnings |
| 20 | warnings.warn( |
| 21 | 'The MEDIA_ROOT setting has been deprecated in favor of USER_MEDIA_ROOT.', |
| 22 | PendingDeprecationWarning |
| 23 | ) |
| 24 | return settings.MEDIA_ROOT |
| 25 | return root |
diff -r 4dd0391a8f6b docs/faq/usage.txt
a
|
b
|
|
48 | 48 | Using a :class:`~django.db.models.FileField` or an |
49 | 49 | :class:`~django.db.models.ImageField` in a model takes a few steps: |
50 | 50 | |
51 | | #. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as |
52 | | the full path to a directory where you'd like Django to store uploaded |
| 51 | #. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT` |
| 52 | as the full path to a directory where you'd like Django to store uploaded |
53 | 53 | files. (For performance, these files are not stored in the database.) |
54 | | Define :setting:`MEDIA_URL` as the base public URL of that directory. |
55 | | Make sure that this directory is writable by the Web server's user |
56 | | account. |
| 54 | Define :setting:`USER_MEDIA_URL` as the base public URL of that |
| 55 | directory. Make sure that this directory is writable by the Web server's |
| 56 | user account. |
57 | 57 | |
58 | 58 | #. Add the :class:`~django.db.models.FileField` or |
59 | 59 | :class:`~django.db.models.ImageField` to your model, making sure to |
60 | 60 | define the :attr:`~django.db.models.FileField.upload_to` option to tell |
61 | | Django to which subdirectory of :setting:`MEDIA_ROOT` it should upload |
62 | | files. |
| 61 | Django to which subdirectory of :setting:`USER_MEDIA_ROOT` it should |
| 62 | upload files. |
63 | 63 | |
64 | | #. All that will be stored in your database is a path to the file |
65 | | (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the |
| 64 | #. All that will be stored in your database is a path to the file (relative |
| 65 | to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the |
66 | 66 | convenience :attr:`~django.core.files.File.url` attribute provided by |
67 | 67 | Django. For example, if your :class:`~django.db.models.ImageField` is |
68 | 68 | called ``mug_shot``, you can get the absolute URL to your image in a |
diff -r 4dd0391a8f6b docs/ref/files/file.txt
a
|
b
|
|
14 | 14 | |
15 | 15 | .. attribute:: File.name |
16 | 16 | |
17 | | The name of file including the relative path from :setting:`MEDIA_ROOT`. |
| 17 | The name of file including the relative path from :setting:`USER_MEDIA_ROOT`. |
18 | 18 | |
19 | 19 | .. attribute:: File.path |
20 | 20 | |
diff -r 4dd0391a8f6b docs/ref/models/fields.txt
a
|
b
|
|
477 | 477 | |
478 | 478 | .. attribute:: FileField.upload_to |
479 | 479 | |
480 | | A local filesystem path that will be appended to your :setting:`MEDIA_ROOT` |
481 | | setting to determine the value of the :attr:`~django.core.files.File.url` |
482 | | attribute. |
| 480 | A local filesystem path that will be appended to your |
| 481 | :setting:`USER_MEDIA_ROOT` setting to determine the value of the |
| 482 | :attr:`~django.core.files.File.url` attribute. |
483 | 483 | |
484 | 484 | This path may contain `strftime formatting`_, which will be replaced by the |
485 | 485 | date/time of the file upload (so that uploaded files don't fill up the given |
… |
… |
|
526 | 526 | Using a :class:`FileField` or an :class:`ImageField` (see below) in a model |
527 | 527 | takes a few steps: |
528 | 528 | |
529 | | 1. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as the |
530 | | full path to a directory where you'd like Django to store uploaded files. |
531 | | (For performance, these files are not stored in the database.) Define |
532 | | :setting:`MEDIA_URL` as the base public URL of that directory. Make sure |
533 | | that this directory is writable by the Web server's user account. |
| 529 | 1. In your settings file, you'll need to define :setting:`USER_MEDIA_ROOT` |
| 530 | as the full path to a directory where you'd like Django to store uploaded |
| 531 | files. (For performance, these files are not stored in the database.) |
| 532 | Define :setting:`USER_MEDIA_URL` as the base public URL of that |
| 533 | directory. Make sure that this directory is writable by the Web server's |
| 534 | user account. |
534 | 535 | |
535 | 536 | 2. Add the :class:`FileField` or :class:`ImageField` to your model, making |
536 | 537 | sure to define the :attr:`~FileField.upload_to` option to tell Django |
537 | | to which subdirectory of :setting:`MEDIA_ROOT` it should upload files. |
| 538 | to which subdirectory of :setting:`USER_MEDIA_ROOT` it should upload files. |
538 | 539 | |
539 | 540 | 3. All that will be stored in your database is a path to the file |
540 | | (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the |
| 541 | (relative to :setting:`USER_MEDIA_ROOT`). You'll most likely want to use the |
541 | 542 | convenience :attr:`~django.core.files.File.url` function provided by |
542 | 543 | Django. For example, if your :class:`ImageField` is called ``mug_shot``, |
543 | 544 | you can get the absolute URL to your image in a template with |
544 | 545 | ``{{ object.mug_shot.url }}``. |
545 | 546 | |
546 | | For example, say your :setting:`MEDIA_ROOT` is set to ``'/home/media'``, and |
| 547 | For example, say your :setting:`USER_MEDIA_ROOT` is set to ``'/home/media'``, and |
547 | 548 | :attr:`~FileField.upload_to` is set to ``'photos/%Y/%m/%d'``. The ``'%Y/%m/%d'`` |
548 | 549 | part of :attr:`~FileField.upload_to` is `strftime formatting`_; ``'%Y'`` is the |
549 | 550 | four-digit year, ``'%m'`` is the two-digit month and ``'%d'`` is the two-digit |
… |
… |
|
555 | 556 | :attr:`~django.core.files.File.name`, :attr:`~django.core.files.File.url` |
556 | 557 | and :attr:`~django.core.files.File.size` attributes; see :ref:`topics-files`. |
557 | 558 | |
| 559 | .. versionchanged:: 1.3 |
| 560 | Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for |
| 561 | file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL`` |
| 562 | |
558 | 563 | Note that whenever you deal with uploaded files, you should pay close attention |
559 | 564 | to where you're uploading them and what type of files they are, to avoid |
560 | 565 | security holes. *Validate all uploaded files* so that you're sure the files are |
diff -r 4dd0391a8f6b docs/ref/settings.txt
a
|
b
|
|
1033 | 1033 | |
1034 | 1034 | Default: ``''`` (Empty string) |
1035 | 1035 | |
1036 | | Absolute path to the directory that holds media for this installation. |
| 1036 | Absolute path to the directory that holds static media for this installation. |
1037 | 1037 | Example: ``"/home/media/media.lawrence.com/"`` See also ``MEDIA_URL``. |
1038 | 1038 | |
| 1039 | .. deprecated:: 1.3 |
| 1040 | This is now used only for backwards compatibility, see ``USER_MEDIA_ROOT`` |
| 1041 | |
1039 | 1042 | .. setting:: MEDIA_URL |
1040 | 1043 | |
1041 | 1044 | MEDIA_URL |
… |
… |
|
1043 | 1046 | |
1044 | 1047 | Default: ``''`` (Empty string) |
1045 | 1048 | |
1046 | | URL that handles the media served from ``MEDIA_ROOT``. |
| 1049 | URL that handles the static media stored in ``MEDIA_ROOT``. |
1047 | 1050 | Example: ``"http://media.lawrence.com"`` |
1048 | 1051 | |
1049 | 1052 | Note that this should have a trailing slash if it has a path component. |
… |
… |
|
1051 | 1054 | Good: ``"http://www.example.com/static/"`` |
1052 | 1055 | Bad: ``"http://www.example.com/static"`` |
1053 | 1056 | |
| 1057 | See also ``USER_MEDIA_URL`` |
| 1058 | |
1054 | 1059 | .. setting:: MIDDLEWARE_CLASSES |
1055 | 1060 | |
1056 | 1061 | MESSAGE_LEVEL |
… |
… |
|
1757 | 1762 | This setting has been replaced by :setting:`TEST_NAME` in |
1758 | 1763 | :setting:`DATABASES`. |
1759 | 1764 | |
| 1765 | .. setting:: USER_MEDIA_ROOT |
| 1766 | |
| 1767 | USER_MEDIA_ROOT |
| 1768 | --------------- |
| 1769 | |
| 1770 | .. versionadded:: 1.3 |
| 1771 | |
| 1772 | Default: ``None`` |
| 1773 | |
| 1774 | Absolute path to the directory that holds user uploaded media for this installation. |
| 1775 | Example: ``"/home/media/media.lawrence.com/user/"`` See also ``USER_MEDIA_URL``. |
| 1776 | |
| 1777 | If ``None``, the value of ``MEDIA_ROOT`` is used as a fallback for this setting. |
| 1778 | |
| 1779 | .. setting:: USER_MEDIA_URL |
| 1780 | |
| 1781 | USER_MEDIA_URL |
| 1782 | -------------- |
| 1783 | |
| 1784 | Default: ``None`` |
| 1785 | |
| 1786 | URL that handles the media stored in ``USER_MEDIA_ROOT``. |
| 1787 | Example: ``"http://media.lawrence.com"`` |
| 1788 | |
| 1789 | Note that this should have a trailing slash if it has a path component. |
| 1790 | |
| 1791 | Good: ``"http://www.example.com/usermedia/"`` |
| 1792 | Bad: ``"http://www.example.com/usermedia"`` |
| 1793 | |
| 1794 | See also ``MEDIA_URL`` |
diff -r 4dd0391a8f6b docs/topics/files.txt
a
|
b
|
|
8 | 8 | |
9 | 9 | This document describes Django's file access APIs. |
10 | 10 | |
11 | | By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and |
12 | | :setting:`MEDIA_URL` settings. The examples below assume that you're using these |
13 | | defaults. |
| 11 | .. versionchanged:: 1.3 |
| 12 | Up to Django 1.2, the ``MEDIA_ROOT`` and ``MEDIA_URL`` settings were used for |
| 13 | file storage, instead of ``USER_MEDIA_ROOT`` and ``USER_MEDIA_URL`` |
| 14 | |
| 15 | By default, Django stores files locally, using the :setting:`USER_MEDIA_ROOT` |
| 16 | and :setting:`USER_MEDIA_URL` settings. The examples below assume that you're |
| 17 | using these defaults. |
14 | 18 | |
15 | 19 | However, Django provides ways to write custom `file storage systems`_ that |
16 | 20 | allow you to completely customize where and how Django stores files. The |
… |
… |
|
125 | 129 | ====================== =================================================== |
126 | 130 | ``location`` Optional. Absolute path to the directory that will |
127 | 131 | hold the files. If omitted, it will be set to the |
128 | | value of your ``MEDIA_ROOT`` setting. |
| 132 | value of your ``USER_MEDIA_ROOT`` setting. |
129 | 133 | ``base_url`` Optional. URL that serves the files stored at this |
130 | 134 | location. If omitted, it will default to the value |
131 | | of your ``MEDIA_URL`` setting. |
| 135 | of your ``USER_MEDIA_URL`` setting. |
132 | 136 | ====================== =================================================== |
133 | 137 | |
134 | 138 | For example, the following code will store uploaded files under |
135 | | ``/media/photos`` regardless of what your ``MEDIA_ROOT`` setting is:: |
| 139 | ``/media/photos`` regardless of what your ``USER_MEDIA_ROOT`` setting is:: |
136 | 140 | |
137 | 141 | from django.db import models |
138 | 142 | from django.core.files.storage import FileSystemStorage |