簡體   English   中英

提交表單為空-Django

[英]Submit form is empty — 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文件的內容都是hehetest request.POST是否為空?

默認情況下,表單提交的方法是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()

    #...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM