gpt4 book ai didi

python - 提交表单为空——Django

转载 作者:行者123 更新时间:2023-12-01 05:09:01 24 4
gpt4 key购买 nike

这是我的模板:

<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 是一个下拉菜单。我的目标是:使用户能够从下拉菜单中选择一个项目,然后在“日期”框中输入数据。这是处理此问题的 View :

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 文件的所有内容都是 hehetestrequest.POST 是否为空?

最佳答案

By default, the method of form submit is 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()

#...

关于python - 提交表单为空——Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24534971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com