我想在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)
更新:
部分设置:使用模板root:
....
# 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
但我可以将样本字符串路径放到值中,然后传输到视图。我会得到同样的问题。
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})