Django django-adminfiles

2019-03-04 17:21发布

问题:

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 pretty simple but I don't understand the 3rd step(because of my bad English level :S):

Make the contents of the adminfiles/static/adminfiles directory available at STATIC_URL/adminfiles. This can be done by through your webserver configuration, via an app such as django.contrib.staticfiles, or by copying the files or making a symlink.

I have run collecstatic, copy the files of adminfiles/static/adminfiles to my static directory and nothing seems to work. When I write a post, it should appear like in this video but the image thumbmails and "All uploads images","Upload","Refresh".... doesn't appear. I'm pretty new to Django and I'm a bit lost with this silly question. ¿Does someone know what i have to do here?

回答1:

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.