2011-06-07 143 views
19

我正在努力使用STATIC_URL變量將媒體拉出模板。例如,我有這樣的代碼STATIC_URL無法正常工作

{% extends "admin/change_list.html" %} 
{% load i18n %} 

{% block extrahead %} 
<!--[if IE]> 
<script type="text/javascript" src="{% firstof STATIC_URL MEDIA_URL %}django_qbe/js/excanvas.js"></script> 
<![endif]--> 
<script type="text/javascript" src="{% firstof STATIC_URL MEDIA_URL %}django_qbe/js/jquery.js"></script> 

每個模板加載它試圖拉斷MEDIA_URL時間。如果我將其更改爲

{% extends "admin/change_list.html" %} 
{% load i18n %} 
{% load static %} 
{% block extrahead %} 
<!--[if IE]> 
<script type="text/javascript" src="{% get_static_prefix %}django_qbe/js/excanvas.js"></script> 
<![endif]--> 
<script type="text/javascript" src="{% get_static_prefix %}django_qbe/js/jquery.js"></script> 

我的問題是爲什麼我的這個模板的第一個版本沒有工作?

+2

你使用RequestContext嗎? – DrTyrsa 2011-06-07 07:22:28

+0

我使用這個應用程序的zip壓縮版本http://versae.github.com/qbe/從我看到他使用RequestContext。 – darren 2011-06-07 07:37:09

+0

我的猜測是你沒有''django.core.context_processors.media「'你的'TEMPLATE_CONTEXT_PROCESSORS'' – DrTyrsa 2011-06-07 07:40:36

回答

56

有一個static context-processor,這是不一樣的media之一。您需要確保您的上下文處理器列表中有django.core.context_processors.static,因此它可以將STATIC_URL添加到上下文中。

+1

當前正在工作的鏈接是https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/#django.core.context_processors.static – 2015-07-15 13:05:40

4

正如我被指責不回答這個問題,讓我澄清我的思維過程:

My question is why doesn't my first version of this template work? 

STATIC_URL is returning a False value 

要找出原因,這裏的步驟,我會用:

  • 嘗試將其打印到您的模板正文 - {{STATIC_URL}}。

  • 檢查settings.py,以確保該值設置 - STATIC_URL = '/靜態/'

檢查你是否有靜態文件設置正確的runserver命令:

https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/ 

供我參考使用:

# 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: "/home/media/media.lawrence.com/static/" 
STATIC_ROOT = '' 

STATIC_URL = '/static/' 

ADMIN_MEDIA_PREFIX = '/static/admin/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'static'), 
) 

或apache別名:

Alias /static /Users/stevenkeith/foo/bar/static 

<Directory /Users/stevenkeith/foo/bar/static> 
    Order deny,allow 
    Allow from all 
</Directory> 

或任何你想要的方法,nginx的,等等

+0

如果你不有一個答案,爲什麼不使用評論? – DrTyrsa 2011-06-07 08:58:17

+2

這是一個答案。它解釋了他/她應該如何調試問題。 – 2011-06-07 09:00:32

+0

更容易閱讀這種方式。 – 2011-06-07 09:00:55

16

從文檔:

如果{{STATIC_URL}}是不是在你的模板工作,你很可能不會呈現模板時使用的RequestContext。

https://docs.djangoproject.com/en/dev/howto/static-files/

+2

這解決了我的問題,但現在我必須將'RequestContext(request)'參數添加到我的'render_to_response'調用中。有沒有更「django」的方式來做到這一點? – Seaux 2013-01-05 19:28:58

+0

你可以使用render()方法,該方法自動使用RequestContext - [鏈接到文檔](https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render) – tatlar 2013-02-08 19:22:51

8

大部分事情已經提及,但編輯和添加一點:

  1. 確保您的settings.py文件正確指定的靜態文件的位置。正如史蒂芬基思寫道,你應該是這樣的:

    # 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: "/home/media/media.lawrence.com/static/" 
    STATIC_ROOT = '' 
    
    STATIC_URL = '/static/' 
    
    ADMIN_MEDIA_PREFIX = '/static/admin/' 
    
    import os 
    # Additional locations of static files 
    STATICFILES_DIRS = (
        os.path.join(os.path.dirname(__file__), 'static'), 
    ) 
    
  2. 然後確保你的TEMPLATE_CONTEXT_PROCESSORS包括「django.core.context_processors.static」。如果settings.py中沒有TEMPLATE_CONTEXT_PROCESSORS項,那麼它默認爲下面的項,並且你都很好。

    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.core.context_processors.debug', 
        'django.core.context_processors.i18n', 
        'django.core.context_processors.media', 
        'django.core.context_processors.static', 
        'django.contrib.auth.context_processors.auth', 
        'django.contrib.messages.context_processors.messages', 
    ) 
    
  3. 確保你使用RequestContext的,當你渲染你的模板。​​這是否默認(參見here),所以你可以只調用

    from django.shortcuts import render 
    
    def myViewFunction(request): 
        return render(request, 'myTemplate.html') 
    

    要小心,因爲django.shortcuts.render_to_response不會爲你做這個,除非你添加一個參數,所以你必須寫像

    from django.shortcuts import render_to_response 
    
    def myViewFunction(request): 
        return render_to_response('myTemplate.html', myContext, context_instance=RequestContext(request)) 
    
6

對我來說,答案是簡單地停止使用STATIC_URL,而是使用以下命令:

我改變了我的從

<link href="{{ STATIC_URL }}css/survey.less" media="screen" rel="stylesheet" type="text/css"/> 

到:

<link href="{% static "css/style.less" %}" media="screen" rel="stylesheet" type="text/less"/> 

而現在它工作正常。簡單得多,我想這也是稍微「更正確」的方式來使用靜態現在(從Django 1.4開始)。請參閱Django docs瞭解更多細節信息。

不要忘記在模板的頂部添加{% load static from staticfiles %}

+0

這個絕對的使用更新的Django版本(v1.6 = <)的方式 – Oz123 2013-12-11 13:23:21