Python Django-包含动态模板

Python Django-包含动态模板,python,django,django-templates,Python,Django,Django Templates,我想用include标记包含动态模板。所以基本上,当我包含带有静态路径的模板时,如下面所示,一切都正常 {% include "stories/Princess_Run_Story/Princess_Run_Story.html" %} 在我的例子中,我将这条路径的值转移。然后使用,完全一样 {% include story_counter %} 但我发现了下面的错误,这里的要点是,这两个值都是相同的,即使我手动从第一个示例中转移了这条路径 Using engine django: djan

我想用
include
标记包含动态模板。所以基本上,当我包含带有静态路径的模板时,如下面所示,一切都正常

{% include "stories/Princess_Run_Story/Princess_Run_Story.html" %}
在我的例子中,我将这条路径的值转移。然后使用,完全一样

{% include story_counter %}
但我发现了下面的错误,这里的要点是,这两个值都是相同的,即使我手动从第一个示例中转移了这条路径

Using engine django:
django.template.loaders.filesystem.Loader: C:\Work-Projects\Web\WebWorkBook\templates\'stories\Cave_Monsters_Story\Cave_Monsters_Story.html' (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Program Files\Python27\lib\site-packages\django\contrib\admin\templates\'stories\Cave_Monsters_Story\Cave_Monsters_Story.html' (Source does not exist)
更新:

部分设置:使用根模板:

....
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'templates')

....
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

ROOT_URLCONF = 'webworkbook.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [MEDIA_ROOT],
        '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',
            ],
        },
    },
]
....
从其他脚本生成的路径的另一部分。使用
os.path.join
,但我甚至可以将示例字符串path放入值中,并转移到视图中。我会得到同样的问题

def view_story(request, story_number):
    try:
        story_counter = 'stories/Princess_Run_Story/Princess_Run_Story.html'
    except Question.DoesNotExist:
        raise Http404("Story does not exist. Something wrong here.")
    return render(request, 'workbook/stories_page.html', {'story_counter': story_counter})

为什么故事前面有撇号?我可以告诉你怎么了。路径中有一个引号。你确定没有声明嵌入单引号的字符串变量吗?@RobertMoskal当然,没有。我只是使用os.path.join和现有文件夹构建路径。你需要显示模板设置;正如前面的评论家所说的,你在字符串中嵌入了额外的引号。在中间有一个带有引号字符的路径是不常见的,所以,是的,这是我对错误的假设。我还看到您在
程序文件下安装了Python,而不是推荐的
c:\python27
。许多程序/模块仍然存在包含空格的路径问题。。。