Changes between Version 2 and Version 3 of CookBookUsingExternalMedia
- Timestamp:
- Nov 30, 2009, 9:02:09 AM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CookBookUsingExternalMedia
v2 v3 8 8 }}} 9 9 10 You would instead insert {{ media_url}} so that it would look like:10 You would instead insert {{ MEDIA_URL }} so that it would look like: 11 11 12 12 {{{ 13 blah blah <img src="{{ media_url}}/img/foo.jpg" alt="this is a foo"> blah blah..13 blah blah <img src="{{ MEDIA_URL }}/img/foo.jpg" alt="this is a foo"> blah blah.. 14 14 }}} 15 15 16 16 It generally requires a big search and replace the first time you implement it. After that, seeing other 17 references in the templates has been enough to remind me to use {{ media_url}} before any static content.17 references in the templates has been enough to remind me to use {{ MEDIA_URL }} before any static content. 18 18 19 We could pass {{ media_url}} in through each view, but since we'll want it throughout the site, it's often easier to19 We could pass {{ MEDIA_URL }} in through each view, but since we'll want it throughout the site, it's often easier to 20 20 set up a simple context processor to do the heavy lifting. 21 22 Note: There is a default media context processor since version 1.0. You can access using {{ MEDIA_URL }} in your templates. 23 24 Then in the settings.py under TEMPLATE_CONTEXT_PROCESSORS, add: 25 "django.core.context_processors.media", 26 27 But if you are still on django 0.96: 21 28 22 29 Here's a example snippet from a context process: 23 30 24 31 {{{ 25 from myproject import constants26 32 from django.conf import settings 27 33 28 34 def common(request): 29 ctx = {} 30 ctx['constants'] = constants 31 ctx['media_url'] = settings.MEDIA_URL 32 ... more stuff here ... 33 return ctx 35 return {'MEDIA_URL': settings.MEDIA_URL} 34 36 }}} 35 37 36 38 Then in the settings.py under TEMPLATE_CONTEXT_PROCESSORS, we added: 37 "my project.context_processors.common",39 "mysite.context_processors.common", 38 40 39 and from there, the MEDIA_URL in settings.py became available in all templates as {{media_url}}. 40 41 Note: There is a default media context processor since revision 5379 and will be included in version 0.97 once it is released. See http://code.djangoproject.com/changeset/5379. You can access using {{ MEDIA_URL }} in your templates. 41 and from there, the MEDIA_URL in settings.py became available in all templates as {{ MEDIA_URL }}.