mirror of
https://github.com/jazzband/django-constance.git
synced 2026-03-16 22:40:24 +00:00
Adding support for using a subdirectory of MEDIA_ROOT for file fields (#475)
* Adding support for using a subdirectory of MEDIA_ROOT for file fields with CONSTANCE_FILE_ROOT setting * Improving documentation for CONSTANCE_FILE_ROOT * Updating PR branch to work with latest master
This commit is contained in:
parent
554dac0473
commit
6a5052e9f4
6 changed files with 28 additions and 5 deletions
|
|
@ -208,4 +208,4 @@ class Config:
|
|||
_meta = Meta()
|
||||
|
||||
|
||||
admin.site.register([Config], ConstanceAdmin)
|
||||
admin.site.register([Config], ConstanceAdmin)
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import hashlib
|
||||
from datetime import date, datetime, time, timedelta
|
||||
from decimal import Decimal
|
||||
from os.path import join
|
||||
|
||||
from django import conf, forms
|
||||
from django.contrib import messages
|
||||
|
|
@ -116,7 +117,7 @@ class ConstanceForm(forms.Form):
|
|||
def save(self):
|
||||
for file_field in self.files:
|
||||
file = self.cleaned_data[file_field]
|
||||
self.cleaned_data[file_field] = default_storage.save(file.name, file)
|
||||
self.cleaned_data[file_field] = default_storage.save(join(settings.FILE_ROOT, file.name), file)
|
||||
|
||||
for name in settings.CONFIG:
|
||||
current = getattr(config, name)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ CONFIG_FIELDSETS = getattr(settings, 'CONSTANCE_CONFIG_FIELDSETS', {})
|
|||
|
||||
ADDITIONAL_FIELDS = getattr(settings, 'CONSTANCE_ADDITIONAL_FIELDS', {})
|
||||
|
||||
FILE_ROOT = getattr(settings, 'CONSTANCE_FILE_ROOT', '')
|
||||
|
||||
DATABASE_CACHE_BACKEND = getattr(
|
||||
settings,
|
||||
'CONSTANCE_DATABASE_CACHE_BACKEND',
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ Note: Use later evaluated strings instead of direct classes for the field and wi
|
|||
'MY_SELECT_KEY': ('yes', 'select yes or no', 'yes_no_null_select'),
|
||||
}
|
||||
|
||||
If you want to work with files you can use this configuration:
|
||||
If you want to work with images or files you can use this configuration:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
|
@ -153,8 +153,14 @@ When used in a template you probably need to use:
|
|||
{% get_media_prefix as MEDIA_URL %}
|
||||
<img src="{{ MEDIA_URL }}{{ config.LOGO_IMAGE }}">
|
||||
|
||||
Images are uploaded to MEDIA_ROOT.
|
||||
Images and files are uploaded to ``MEDIA_ROOT`` by default. You can specify a subdirectory of ``MEDIA_ROOT`` to use instead by adding the ``CONSTANCE_FILE_ROOT`` setting. E.g.:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
CONSTANCE_FILE_ROOT = 'constance'
|
||||
|
||||
This will result in files being placed in ``media/constance`` within your ``BASE_DIR``. You can use deeper nesting in this setting (e.g. ``constance/images``) but other relative path components (e.g. ``../``) will be rejected.
|
||||
|
||||
Ordered Fields in Django Admin
|
||||
------------------------------
|
||||
|
|
|
|||
|
|
@ -99,7 +99,8 @@ CONSTANCE_ADDITIONAL_FIELDS = {
|
|||
}
|
||||
],
|
||||
'email': ('django.forms.fields.EmailField',),
|
||||
'json_field': ['cheeseshop.fields.JsonField']
|
||||
'json_field': ['cheeseshop.fields.JsonField'],
|
||||
'image_field': ['django.forms.ImageField', {}],
|
||||
}
|
||||
|
||||
CONSTANCE_CONFIG = {
|
||||
|
|
@ -115,6 +116,11 @@ CONSTANCE_CONFIG = {
|
|||
'Some test data for json',
|
||||
'json_field',
|
||||
),
|
||||
'LOGO': (
|
||||
'',
|
||||
'Logo image file',
|
||||
'image_field',
|
||||
),
|
||||
}
|
||||
|
||||
CONSTANCE_CONFIG_FIELDSETS = {
|
||||
|
|
@ -124,6 +130,7 @@ CONSTANCE_CONFIG_FIELDSETS = {
|
|||
'OWNER_EMAIL',
|
||||
'MUSICIANS',
|
||||
'DATE_ESTABLISHED',
|
||||
'LOGO',
|
||||
],
|
||||
'Awkward test settings': ['MY_SELECT_KEY', 'MULTILINE', 'JSON_DATA'],
|
||||
}
|
||||
|
|
@ -158,4 +165,10 @@ USE_TZ = True
|
|||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
MEDIA_URL = '/media/'
|
||||
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
CONSTANCE_FILE_ROOT = 'constance'
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
Django>=3.2
|
||||
Pillow
|
||||
pymemcache
|
||||
|
|
|
|||
Loading…
Reference in a new issue