I\'m creating a Blog with Django and I need to insert images on the posts. As far as I know, the best package for that is django-adminfiles
The installation guide is pre
Install
'adminfiles' :
pip install django-adminfiles==1.0.1
'sorl-thumbnail' :
pip install sorl-thumbnail==12.2
Prerequisites for 'pillow' :
yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel -y
'pillow' :
pip install pillow==2.8.1
Use adminfiles
I have created a django project named 'demo', it has only one app 'trial'.
demo | |------ demo | |---- __init__.py | |---- settings.py | |---- urls.py | |---- wsgi.py | |------ manage.py |------ trial |---- \aa___init__.py |---- admin.py |---- models.py |---- tests.py |---- views.pyTo use adminfiles in the 'demo' project we must do the following:
Add 'adminfiles', 'sorl.thumbnail' to INSTALLED_APPS tuple of demo/settings.py file.
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'adminfiles', 'sorl.thumbnail', 'trial', )
Add adminfiles url to the urlatterns list of demo/urls.py file.
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^adminfiles/', include('adminfiles.urls')), ]
Modify admin of trial/admin.py file.
(Note: Article is a model defined in trial/models.py, it has a TextField field named 'text')
from django.contrib import admin from models import Article from adminfiles.admin import FilePickerAdminadmin.site.register(Article)class ExtraAdmin(FilePickerAdmin): adminfiles_fields = ('text',) admin.site.register(Article, ExtraAdmin)
python manage.py migrate
Set MEDIA_URL and MEDIA_ROOT.
demo/settings.py :
MEDIA_URL = '/media/' MEDIA_ROOT = '/data/media/'
demo/urls.py :
from django.conf import settings from django.conf.urls.static import static urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^adminfiles/', include('adminfiles.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After the above settings, the upload files will be stored in '/data/media/adminfiles/' dir.