简体   繁体   English

Django:无法在网页中包含静态文件

[英]Django : Cant include static files in webpage

I know there are a few questions like this, but none of them have worked for me. 我知道有几个这样的问题,但是没有一个对我有用。 I have django version 1.9.2. 我有Django版本1.9.2。 The documentation was not of much help wither as i found it quite confusing. 该文档没有太多帮助,因为我发现它很混乱。 Here is my set up 这是我的设置

myApp-

      static-
              myApp-
                    scripts-
                            scripts.js
                    images-
                            xyz.png
      templates-
              myApp-

                    index.html

in my settings.py i have 在我的settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'campaign.apps.CampaignConfig'
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

STATIC_URL = '/static/'

and my webpage has 我的网页上有

<script language="javascript">var STATIC_URL = '/static/';</script>
<script src="{{ STATIC_URL }}myApp/scripts/scripts.js"></script>

and this does not get recognized. 这并没有得到认可。 neither does this 这也不

<img src="{{ STATIC_URL }}myApp/images/xyz.png">

what other changes do i need to make? 我还需要进行哪些其他更改? Is this the correct approach? 这是正确的方法吗?

You should be using the static template tag 您应该使用static模板标签

{% load static %}
<img src="{% static 'myApp/images/xyz.png' %}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM