我是Django的新手,我有以下问题
使用views.py,我会渲染一个名为' results_links.html'位于应用程序模板目录中,其中包含' include'对另一页的声明' MAPE_history.csv.html'位于'上传'目录(在settings.py中也定义为模板目录)。我想动态传递要在'包含'中使用的文件。来自views.py的声明。对于测试,我使用相同的文件:MAPE_history.csv.html 我遇到的问题是,我从include语句中收到了一个“不存在”错误的'错误。如果我删除了我更换“内容”的声明。变量与文件名,我没有收到问题。 这是代码:
### views.py
from django.shortcuts import render
from django.http import HttpResponse
import os
from formupload import Best_model_forecast_v11
# Create your views here.
def home(request):
return render(request, 'index.html', {'what':'Django File Upload'})
def upload(request):
if request.method == 'POST':
handle_uploaded_file(request.FILES['file'], str(request.FILES['file']))
MAPE_file = 'MAPE_'+str(request.FILES['file'])+'.html'
return render(request,'result_links.html',{'content':MAPE_file})
elif request.method == 'GET':
return render(request,'result_links.html',{'content':'MAPE_history.csv.html'})
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'upload/MAPE_history.csv; filename="MAPE_history.csv"'
return response
return HttpResponse("Failed")
def handle_uploaded_file(file, filename):
if not os.path.exists('upload/'):
os.mkdir('upload/')
if not os.path.exists('forecast/'):
os.mkdir('forecast/')
with open('upload/' + filename, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
#print(Best_model_forecast_v11.main('upload/' + filename))
return Best_model_forecast_v11.main('upload/' + filename)
### result_links.html
<html>
<head>
<title>Rezultate</title>
<IMG SRC="logo.jpg" ALT="" WIDTH=100 HEIGHT=100>
</head>
<a href="D:/FLORIAN/Django/mysite/upload/MAPE_history.csv.html">forecast</a>
{% block content %}
{% include 'logo.html' %}
{% include '{content}' %} # 'template not existing' error is coming from this statement!
<table>
<tr>
<td>
<form action="{{ request.build_absolute_uri }}" method="GET" enctype="multipart/form-data">
<input type="file" name="{content}"/>
<br />
<input type="submit" value="Rezultate" />
</form>
</td>
</tr>
</table>
<a href='{{ MEDIA_URL }}{{ content.relative_path }}'>{{content}}</a>
{% endblock %}
</html>
### settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates','d:/florian/django/mysite/upload/'),
'd:/florian/django/mysite/upload/'
],
'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',
],
},
},
]
### error
TemplateDoesNotExist at /upload/
{content}
Request Method: POST
Request URL: http://localhost:8000/upload/
Django Version: 1.11.7
Exception Type: TemplateDoesNotExist
Exception Value:
{content}
Exception Location: C:\Users\lored\Envs\simpleform\lib\site-packages\django\template\engine.py in find_template, line 148
Python Executable: C:\Users\lored\Envs\simpleform\Scripts\python.exe
Python Version: 3.5.0
Python Path:
['D:\\FLORIAN\\Django\\mysite',
'C:\\Users\\lored\\Envs\\simpleform\\Scripts\\python35.zip',
'C:\\Users\\lored\\Envs\\simpleform\\DLLs',
'C:\\Users\\lored\\Envs\\simpleform\\lib',
'C:\\Users\\lored\\Envs\\simpleform\\Scripts',
'c:\\python35\\Lib',
'c:\\python35\\DLLs',
'C:\\Users\\lored\\Envs\\simpleform',
'C:\\Users\\lored\\Envs\\simpleform\\lib\\site-packages']
Server time: Tue, 28 Nov 2017 18:27:27 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: d:\florian\django\mysite\upload\{content} (Source does not exist)
django.template.loaders.filesystem.Loader: d:\florian\django\mysite\upload\{content} (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\lored\Envs\simpleform\lib\site-packages\django\contrib\admin\templates\{content} (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\lored\Envs\simpleform\lib\site-packages\django\contrib\auth\templates\{content} (Source does not exist)
django.template.loaders.app_directories.Loader: D:\FLORIAN\Django\mysite\formupload\templates\{content} (Source does not exist)
模板目录
答案 0 :(得分:1)
模板名称可以是单引号或双引号中的变量或硬编码(带引号)字符串。
尝试使用{% include content %}
答案 1 :(得分:0)
像这样访问Django中的变量需要双括号;将您的{content}行更改为{{content}}。
顺便说一句,请检查您的&#34; elif request.method ==&#39; GET&#39;:&#34;代码,因为永远不会达到第二次回报。