python - 提交表单为空——Django

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

相关文章:

python - 为什么python print(09)给出SyntaxError : invalid token but not print(07)?

python - 如何使用 GCP 中的计算 API 列出具有特定标签的实例?

python - Django : Foreign key set not found after saving object

django - 删除未使用的模型,陈旧的内容类型提示

Ubuntu 上的 Python : 'module' object has no attribute 'c_bool'

javascript - HTML5 Canvas Draw Rect - 得到不同宽度的边框?

css - float 图像伸出 div 底部

javascript - 如何使网站的 HTML 设计具有可扩展性、可重用性和灵活性?

python - 通过反向过滤查询集在 Django 中存在检查

python - 当子列表的大小依赖于数据值(种子和扩展)时,将列表拆分为子列表