提交表单为空 -- Django
这是我的模板:
<form action="{% url "calculate" %}>
<label2>
<select name="ASSETS_filn">
<option selected>Files</option>
{% for document in documents %}
<option>{{ document.filename }}</option>
{% endfor %}
</select>
</label2>
<br>
<label>Date</label>
<input class="button3" type="text" name="DATE_val" />
<input class="button3" type="submit" value="Calculate" />
</form>
label2 是一个下拉菜单。我的目标是:让用户可以从下拉菜单中选择一个项目,并在日期框中输入数据。处理这个的视图是:
def calculate(request):
os.chdir(settings.PROJECT_PATH + '/calc/')
f = open('calc_log.txt', 'w') # Could change to 'a' for user activity log
f.write("hehehehe")
for key in request.POST:
f.write(str(key) + " " + str(request.POST[key]) + '\n')
f.write('\n\n')
f.write("test")
f.close()
return render( #...
但是写入 .txt
文件的内容只有 hehe
和 test
。那 request.POST
是不是空的呢?
1 个回答
2
默认情况下,表单提交的方法是 GET
,但你想用 POST
方法。
所以,你需要指定使用的方法:
<form action="{% url 'calculate' %}" method="POST">
另外,检查一下使用的方法也是个好主意:
def calculate(request):
if request.method == "POST":
os.chdir(settings.PROJECT_PATH + '/calc/')
f = open('calc_log.txt', 'w') # Could change to 'a' for user activity log
f.write("hehehehe")
for key in request.POST:
f.write(str(key) + " " + str(request.POST[key]) + '\n')
f.write('\n\n')
f.write("test")
f.close()
#...