简体   繁体   English

Django 模板解耦:无法访问 Django 项目之外的静态文件

[英]Django template decoupling : cant access to the static files outside of Django project

My app is decoupled between APIs and templates.我的应用程序在 API 和模板之间解耦。 I want to know the way to communicate the APIs to templates.我想知道将 API 与模板通信的方式。

I know I can pull all the static files into API repo by python manage.py collectstatic , but I dont want to couple APIs and templates together into one folder.我知道我可以通过python manage.py collectstatic将所有静态文件拉入 API 存储库,但我不想将 API 和模板耦合到一个文件夹中。

I am trying to access the static files from my django app to the static files that are outside of my django app.我试图从我的 django 应用程序访问静态文件到我的 django 应用程序之外的静态文件。 I want to access the files in UIUX/static to DjangoRepo/myapp/static.我想将 UIUX/static 中的文件访问到 DjangoRepo/myapp/static。 it looks like below它看起来像下面

UIUX/
 - static
  - staticfile...
 - templates
  - index.html
DjangoRepo/
 - myapp
  - static
  - templates

my view.py我的观点.py

class IndexViewTemplateView(View):
    # trying to acess the inedx.html file that is outside of Django file
    template_name = '../../../UIUX/templates/index.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)

    def post(self, request, *args, **kwargs):
        return render(request, self.template_name)

I did not edit my template at all for this.我根本没有为此编辑我的模板。 is there any other template setting I need to do to archive this ?我需要做任何其他模板设置来存档吗?

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(PROJECT_ROOT, 'templates')],
        # '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',
            ],
            # 'debug': DEBUG,
        },
    },
]

让我知道您的 PROJECT_ROOT 和 STATICFILES_DIRS。

将此行从 - 'DIRS': [os.path.join(PROJECT_ROOT, 'templates')] 更改为 - 'DIRS': [os.path.join(PROJECT_ROOT,'templates'),os.path.join( 'C:/Users/kevin/workspace/UIUX/static')]。

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

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