Failed to pass html input to django

39 views Asked by At

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.

2

There are 2 answers

0
Iliano 920 On BEST ANSWER

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>
0
Jamil Matheny On

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>