Home > database >  Failed to pass html input to django
Failed to pass html input to django

Time:05-09

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

CodePudding user response:

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>

CodePudding user response:

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 ><a href="{% url 'daily_usage_report' %}" style="color: white;">Daily Usage Report</a></button>
</form>
  • Related