jford
jford

Reputation: 147

Generate report from Django based on user input

I'm trying to generate a report from Django based on radio buttons in a template, but having trouble getting data back from the template to determine which variant of the report should be generated.

Template snippet:

<form action="{% url projects.views.projectreport reporttype %}">
    {% csrf_token %}
    <p>
    <input type="radio" name="reporttype"  value="All">All<br>
    <input type="radio" name="reporttype"  value="Current">Current</p>
    <input type = "submit" value="Print Project Report">
    </form>

View snippet:

 reporttype = 'all'
    if 'current' in request.POST:
        reporttype = 'current'
    return render_to_response('index.html',{'project_list': project_list, 'reporttype': reporttype}, context_instance=RequestContext(request))

I can return a value from the template to the same view, but this goes to a different view (projects.views.projectreport). I'm probably doing something really basic wrong...

J.

Upvotes: 1

Views: 509

Answers (1)

sberry
sberry

Reputation: 131978

It wouldn't be "current" that is in request.POST, it would be reporttype. request.POST is a dictionary-like object, so checking in will check keys, not values. The value of reporttype could be 'Current' or 'All'. So just change your code so

reporttype = request.POST['reporttype']

This will set reporttype to be either All or Current (assuming you have a default set in the html - which currently you do not). You could also do what you were attempting by doing

reporttype = request.POST.get('reporttype', 'All').lower()

which will set the value to either the value passed in from the radio button, or to the default 'All'. It also appears you want it lower cased, so sticking lower() on the end should handle that for you.

Upvotes: 2

Related Questions