我渲染一个模板并返回它。程序似乎只渲染一次模板,然后即使我更改输入参数,它也会返回第一个渲染的模板。名为 getNumber 的函数工作正常,每次返回不同的数据。所以基本上问题是模板总是用“解决方案:0”生成的。如果初始数据为 0.1,则生成的模板将始终包含“Solution 0.1”。
我的模板:
<html>
<head>
<meta charset='UTF-8'>
<title>file uploader</title>
</head>
<body>
<form id='selectForm'>
<input id='selectInput' type='file' name='files' multiple>
<div>Files:</div>
<table id='selectTable'></table>
</form>
<script>
...
</script>
<p> Solution: {{ solution }} </p>
</body>
</html>
在 views.py 中返回 HttpResponse 解析'/' url的函数:
def index(request):
if request.method == 'POST':
uploadFile1 = request.FILES['file1']
uploadFile2 = request.FILES['file2']
text1 = ''
text2 = ''
with open('file1', 'wb+') as file1:
text1 = uploadFile1.read()
with open('file1', 'wb+') as file2:
text2 = uploadFile2.read()
data = calcpy.views.getNumber(str(text1), str(text2))
print(data['number'])
template = loader.get_template('main.html')
context = {'solution' : data['number']}
template.render(context);
return HttpResponse(template.render(context))
template = loader.get_template('main.html')
context = {'solution' : 0}
return HttpResponse(template.render(context))
settings.py 中的代码负责模板。我想这是错误的。
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True
},
]
# Application definition
INSTALLED_APPS = (
'version',
'current',
'calcpy',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
)
我使用 django 1.9.5,我无法真正更改版本。