{"id":171,"date":"2012-06-07T15:54:32","date_gmt":"2012-06-07T19:54:32","guid":{"rendered":"http:\/\/lichun.cc\/blog\/?p=171"},"modified":"2012-06-07T15:54:32","modified_gmt":"2012-06-07T19:54:32","slug":"fix-static_url-is-empty-in-django","status":"publish","type":"post","link":"https:\/\/www.lichun.cc\/blog\/2012\/06\/fix-static_url-is-empty-in-django\/","title":{"rendered":"fix STATIC_URL is empty in Django"},"content":{"rendered":"<p>When using static files in my Django application, the STATIC_URL is empty even I set it in the setting.py, after a few research, here is how I fixed it.<\/p>\n<p>My static image files are located at <b>movie\/static\/img<\/b> (my application is <b>movie<\/b>)<\/p>\n<p>1. in views.py<\/p>\n<pre>\nfrom django.template import RequestContext\n<\/pre>\n<p>if you use render_to_response, make sure you add <code>context_instance=RequestContext(request)<\/code> in it,<\/p>\n<pre>render_to_response('movie\/index.html', {'movie_list': movie_list}, context_instance=RequestContext(request));<\/pre>\n<p><!--more--><\/p>\n<p>2. in settings.py, make sure there are following lines:<\/p>\n<pre>\nSTATIC_URL = '\/static\/'\n\nTEMPLATE_CONTEXT_PROCESSORS = (\n'django.core.context_processors.debug',\n'django.core.context_processors.i18n',\n'django.core.context_processors.media',\n'django.core.context_processors.static',\n'django.contrib.auth.context_processors.auth',\n'django.contrib.messages.context_processors.messages',\n)\n<\/pre>\n<p>3. in template, the usage is:<\/p>\n<pre>&lt;img src=\"{{ STATIC_URL }}img\/{{ movie.image }}\" \/&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When using static files in my Django application, the STATIC_URL is empty even I set it in the setting.py, after a few research, here is how I fixed it. My static image files are located at movie\/static\/img (my application is movie) 1. in views.py from django.template import RequestContext if you use render_to_response, make sure you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[26],"tags":[84,29,5],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2s9sh-2L","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts\/171"}],"collection":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":0,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}