Django django-adminfiles

后端 未结 1 942
南方客
南方客 2021-01-27 14:19

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

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 15:03

    My environment

    • OS: CentOS7_x86_64
    • Python: 2.7.5 (must be devel version)
    • Django: 1.8.1

    Step

    1. 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

    2. 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.py
          
      To 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 FilePickerAdmin
        
        admin.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.

    0 讨论(0)
提交回复
热议问题