STATIC_URL无效

dar*_*ren 20 django

我正在努力使用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>
Run Code Online (Sandbox Code Playgroud)

每次加载模板时,都会尝试拉出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>
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么我的第一个版本的模板不起作用?

Joh*_*n C 60

有一个静态上下文处理器,它与那个不同media.您需要确保django.core.context_processors.static在上下文处理器列表中有,因此它可以添加STATIC_URL到上下文中.


Jon*_*han 17

来自文档:

如果{{STATIC_URL}}在模板中不起作用,则在渲染模板时可能不会使用RequestContext.

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

  • 这解决了我的问题,但现在我必须在``render_to_response`调用中添加`RequestContext(request)`参数.有没有更"django"的方式来做到这一点? (2认同)

小智 8

大多数事情已经被提及但是要编译并添加一点:

  1. 确保您的settings.py文件正确指定了静态文件的位置.正如Steven Keith所写,你应该有类似的东西:

    # 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'),
    )
    
    Run Code Online (Sandbox Code Playgroud)
  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',
    )
    
    Run Code Online (Sandbox Code Playgroud)
  3. 确保在渲染模板时使用RequestContext.django.shortcuts.render默认情况下这样做(见这里),所以你可以打电话

    from django.shortcuts import render
    
    def myViewFunction(request):
        return render(request, 'myTemplate.html')
    
    Run Code Online (Sandbox Code Playgroud)

    小心,因为除非你添加一个参数,否则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))
    
    Run Code Online (Sandbox Code Playgroud)


gro*_*wlf 7

对我来说,答案只是停止使用STATIC_URL,而是使用以下内容:

我改变了我的意见

<link href="{{ STATIC_URL }}css/survey.less" media="screen" rel="stylesheet" type="text/css"/>
Run Code Online (Sandbox Code Playgroud)

至:

<link href="{% static "css/style.less" %}" media="screen" rel="stylesheet" type="text/less"/>
Run Code Online (Sandbox Code Playgroud)

现在它工作正常.更简单,我怀疑这也是现在使用静态(从Django 1.4开始)的稍微"更正确"的方式.有关详细信息,请参阅Django文档.

别忘了{% load static from staticfiles %}在模板的顶部添加也使用它.