Django MEDIA_URL and MEDIA_ROOT

前端 未结 13 1057
天命终不由人
天命终不由人 2020-11-22 11:36

I\'m trying to upload an image via the Django admin and then view that image either in a page on the frontend or just via a URL.

Note this is all on my local machine

相关标签:
13条回答
  • 2020-11-22 12:14

    UPDATE for Django >= 1.7

    Per Django 2.1 documentation: Serving files uploaded by a user during development

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = patterns('',
        # ... the rest of your URLconf goes here ...
    ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    You no longer need if settings.DEBUG as Django will handle ensuring this is only used in Debug mode.


    ORIGINAL answer for Django <= 1.6

    Try putting this into your urls.py

    from django.conf import settings
    
    # ... your normal urlpatterns here
    
    if settings.DEBUG:
        # static files (images, css, javascript, etc.)
        urlpatterns += patterns('',
            (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT}))
    

    With this you can serve the static media from Django when DEBUG = True (when you run on local computer) but you can let your web server configuration serve static media when you go to production and DEBUG = False

    0 讨论(0)
  • 2020-11-22 12:14

    This is what I did to achieve image rendering in DEBUG = False mode in Python 3.6 with Django 1.11

    from django.views.static import serve
    urlpatterns = [
    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    # other paths
    ]
    
    0 讨论(0)
  • 2020-11-22 12:17

    Here What i did in Django 2.0. Set First MEDIA_ROOT an MEDIA_URL in setting.py

    MEDIA_ROOT = os.path.join(BASE_DIR, 'data/') # 'data' is my media folder
    MEDIA_URL = '/media/'
    

    Then Enable the media context_processors in TEMPLATE_CONTEXT_PROCESSORS by adding

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                #here add your context Processors
                'django.template.context_processors.media',
            ],
        },
    },
    ]
    

    Your media context processor is enabled, Now every RequestContext will contain a variable MEDIA_URL.

    Now you can access this in your template_name.html

    <p><img src="{{ MEDIA_URL }}/image_001.jpeg"/></p>
    
    0 讨论(0)
  • 2020-11-22 12:19

    Your setting is all right. Some web servers require to specify the media and static folder files specifically. For example in pythonanywhere.com you have to go to the 'Web' section and add the url od the media folders and static folder. For example:

      URL                     Directory                
    /static/            /home/Saidmamad/discoverthepamirs/static     
    /accounts/static/   /home/Saidmamad/discoverthepamirs/accounts/static    
    /media/            /home/Saidmamad/discoverthepamirs/discoverthepamirs/media    
    

    I know that it is late, but just to help those who visit this link because of the same problem ;)

    0 讨论(0)
  • 2020-11-22 12:21

    (at least) for Django 1.8:

    If you use

    if settings.DEBUG:
      urlpatterns.append(url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
    

    as described above, make sure that no "catch all" url pattern, directing to a default view, comes before that in urlpatterns = []. As .append will put the added scheme to the end of the list, it will of course only be tested if no previous url pattern matches. You can avoid that by using something like this where the "catch all" url pattern is added at the very end, independent from the if statement:

    if settings.DEBUG:
        urlpatterns.append(url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
    
    urlpatterns.append(url(r'$', 'views.home', name='home')),
    
    0 讨论(0)
  • 2020-11-22 12:25

    Adding to Micah Carrick answer for django 1.8:

    if settings.DEBUG:
      urlpatterns.append(url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
    
    0 讨论(0)
提交回复
热议问题