0

This is as I understand the way Django works.
From the address bar in the browser the urls.py is searched for a matching address. If address is found the next step is access a def() in the views.py The def() does stuff and passes data back to the web page.

What I want to do, and failing to understand the mechanism to do so, is to take two input dates from the web page and pass them to the views.def(). Does submit on the web page store more that one data entry. If so, how does this work?

test.html

{% extends 'base.html' %}
{% block content %}
    <br /><br />
    <form action="" method="get">   
        <label for="from">Start Date</label>
            <input type="text" name="start_date"><br /><br />
        <label for="to">End Date &nbsp;</label>
            <input type="text" name="end_date"><br />
        <input type="submit" value="Submit">
    </form>
    <p>{{ s_date }}</p>
    <p>{{ e_date }}</p>
{% endblock %}

views.py

def Test(request):
    if 'start_date' in request.GET: 
        s_date = request.GET['start_date']
        e_date = request.GET['end_date']
    else:
        s_date = None
        e_date = None
    context = {"s_date": s_date, "e_date": e_date}
    return render_to_response('test.html', context, context_instance=RequestContext(request))

On the Stackoverflow message Passing objects from template to view using Django method="post" is used. As I only want to read the data would method="get" be better?

Just in case, the dates are stores in yyyy-mm-dd format.

The function is working. I would like to know what I am missing regarding the passing of the variables from template to function.

Thank you.

1 Answer 1

1

You can access GET parmeters using request.GET, for example request.GET['start_date'] and request.GET['end_date'].

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Ludwik. Apologies but I do not understand what it is that you mean. Could you expand on your explanation, please?
I showed you how you can access the data send from your form in your view. What exactly is not clear to you?
I have to go back to level 0 for this one, if you please. Is the above html code likely to pass "start_date" and "end_date" values back to the function def BetweenDates(request)? Do I need extra arguments like "def get_hotel_sum_quantity(self, product_id, checkin_date, checkout_date):" as seen in stackoverflow.com/questions/8429407/… ? I cannot see an example of how to pass two dates from the template back to the function works in the Django Books v2.
Yes. The HTML code will work, and you don't need extra arguments on your view function. Just use the request.GET dictionary-like object to access those values - as I showed in my answer.
Like the Kerryman who stayed up night to see where the Sun went. Finally, it dawned on him... Code updated. Thank you Ludwick for sticking with me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.