Django服务静态文件/angularJS应用程序

2024-10-02 20:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Django rest框架编写了一个restapi,并以angularjs应用程序作为前端。我还使用gulp来构建我的angularjs应用程序的压缩版本,并将其输出到django应用程序的静态文件目录中。我尝试使用django templateview,但这会导致控制台错误:

vendor-001932f856.js:1 Uncaught SyntaxError: Unexpected token <
app-2d5e1cec88.js:1 Uncaught SyntaxError: Unexpected token <

这两个文件都是有效的javascript文件。这是我的URL文件:

^{pr2}$

这些是我的静态文件和模板设置:

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'static/eventshop'),
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'static/eventshop')],
        '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',
            ],
        },
    },
]

我的angular应用程序位于static/eventshop和索引.html是相对的URL(因此没有静态的URL前缀)。在

为什么它会给我这个错误和/或者在django中提供一个“静态”角度应用程序的最佳方式是什么?在


Tags: 文件pathdjango应用程序urlbaseosdir
2条回答

你自己的答案很有帮助,但我发现我必须在我的设置.py为了让它发挥作用-我最后用的是:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')#typical usage from docs
STATIC_ROOT = os.path.join(BASE_DIR, 'static')#this in both your/my case
STATIC_URL = '/static/'
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'static')#same as static_root
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
WHITENOISE_INDEX_FILE = True

使用django-whitenoise并将whitenoise的根路径设置为静态文件对我很有效。在

相关问题 更多 >