Confusion in Django admin, static and media files

后端 未结 3 1717
别那么骄傲
别那么骄傲 2020-11-27 15:25

I\'m relatively new to Django (1.4) and I\'m having a hard time understanding the philosophy behind static, media, and admin files. The structure of the project is different

3条回答
  •  盖世英雄少女心
    2020-11-27 16:12

    My config is:

    1.settings.py

        # Absolute filesystem path to the directory that will hold user-uploaded files.
        # Example: "/var/www/example.com/media/"
        MEDIA_ROOT='/media/'
    
        # URL that handles the media served from MEDIA_ROOT. Make sure to use a
        # trailing slash.
        # Examples: "http://example.com/media/", "http://media.example.com/"
        MEDIA_URL = '/media/'
    
        # Absolute path to the directory static files should be collected to.
        # Don't put anything in this directory yourself; store your static files
        # in apps' "static/" subdirectories and in STATICFILES_DIRS.
        # Example: "/var/www/example.com/static/"
        STATIC_ROOT = '/static/'
    
        # URL prefix for static files.
        # Example: "http://example.com/static/", "http://static.example.com/"
        STATIC_URL = '/static/'
    
    
        # Additional locations of static files
        STATICFILES_DIRS = (
            '/'.join(__file__.split(os.sep)[0:-2]+['static']),
            # Put strings here, like "/home/html/static" or "C:/www/django/static".
            # Always use forward slashes, even on Windows.
            # Don't forget to use absolute paths, not relative paths.
        )
    
        # List of finder classes that know how to find static files in
        # various locations.
        STATICFILES_FINDERS = (
            'django.contrib.staticfiles.finders.FileSystemFinder',
            'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        #    'django.contrib.staticfiles.finders.DefaultStorageFinder',
        )
    

    2.urls.py

        from django.conf import settings
        if settings.DEBUG:
           from django.contrib.staticfiles.urls import staticfiles_urlpatterns
           urlpatterns += staticfiles_urlpatterns()
    

    AND my site dir is like this:

        root
        │  manage.py
        │
        ├─media
        ├─my_django_py3
        │    settings.py
        │    urls.py
        │    views.py
        │    wsgi.py
        │    __init__.py
        │
        ├─static
        │      9gq05.jpg
        │      ajax.js
        │      favicon.gif
        │
        ├─templates
        └─utils
    

提交回复
热议问题