Ricky
Ricky

Reputation: 53

Failed to pass html input to django

I'm trying to pass date input to django python script in order to retrieve the data based on it.

Here is my code.

view

def daily_usage_report(request):
    if request.method == 'POST':
        request_date = request.POST.get('request_date')
    else:
        request_date = date.today()

    print(request_date)
...

html

    <form method="post" action="#">
        <input type="date" name="request_date" id="request_date">
        <button class="btn btn-primary"><a href="{% url 'daily_usage_report' %}" style="color: white;">Daily Usage Report</a></button>
    </form>

I tried to fill the form with random date, but it keeps printing today date.

Upvotes: 1

Views: 38

Answers (2)

Jamil Matheny
Jamil Matheny

Reputation: 160

You need to pass the python script file in the POST form method action in your HTML script.


<form method="post" action="python_script_goes_here.py">
        <input type="date" name="request_date" id="request_date">
        <button class="btn btn-primary"><a href="{% url 'daily_usage_report' %}" style="color: white;">Daily Usage Report</a></button>
</form>

Upvotes: 0

Iliano 920
Iliano 920

Reputation: 36

your html should be looking like this:

<form method="post" action="{% url 'daily_usage_report' %}">
    <input type="date" name="request_date" id="request_date">
    <input type="submit">
</form>

Upvotes: 2

Related Questions