Django {{ MEDIA_URL }} blank @DEPRECATED

前端 未结 5 1843
不思量自难忘°
不思量自难忘° 2020-12-01 13:55

I have banged my head over this for the last few hours. I can not get {{ MEDIA_URL }} to show up

in settings.py

..
MEDIA_URL = \'http://10.10.0.106/a         


        
相关标签:
5条回答
  • 2020-12-01 14:18

    Adding media template context processor also gets the job done

    TEMPLATE_CONTEXT_PROCESSORS = (
        "django.core.context_processors.request",
        "django.contrib.auth.context_processors.auth",
        "django.core.context_processors.media",
        "django.core.context_processors.static",
    )
    
    0 讨论(0)
  • 2020-12-01 14:23

    You can also use direct_to_template:

    from django.views.generic.simple import direct_to_template
    ...
    return direct_to_template(request, 'Question/latest.html', {'latest': p})
    
    0 讨论(0)
  • 2020-12-01 14:25

    In addition to question provided above can suggest you to take a look at photologue application. It could help you to avoid direct links in template files and use objects instead. F.ex.:

    <img src="{{ artist.photo.get_face_photo_url }}" alt="{{ artist.photo.title }}"/>
    
    0 讨论(0)
  • 2020-12-01 14:25

    Update: For Django 1.10 users, the both media and static context processors are already moved in django.template from django.core read the following article for more info: https://docs.djangoproject.com/en/1.10/ref/templates/api/#django-template-context-processors-media

    0 讨论(0)
  • 2020-12-01 14:27

    You need to add the RequestContext in your render_to_response for the context processors to be processed.

    In your case:

    from django.template.context import RequestContext
    
    context = {'latest': p}
    render_to_response('Question/latest.html',
                       context_instance=RequestContext(request, context))
    

    From the docs:

    context_instance

    The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead.

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