KSHMR
KSHMR

Reputation: 809

Submit form is empty -- Django

This is my template:

<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 is a dropdown menu. My objective with this is: Enable the user to select an item from the dropdown menu and enter data into the Date box. This is the view that handles this:

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( #...

But all that is written to the .txt file is hehe and test. Is request.POST empty?

Upvotes: 1

Views: 810

Answers (1)

karthikr
karthikr

Reputation: 99680

By default, the method of form submit is GET, and you intend to do a POST.

So, specify the method:

<form action="{% url 'calculate' %}" method="POST">

Also, it is a good idea to check for the method:

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()

    #...

Upvotes: 2

Related Questions