Zoojay
Zoojay

Reputation: 143

How to use data retrieved a form in Django (for a calculator app)

I'm new to Django. Even after reading, watching, and following many tutorials and guides, things still aren't coming together for me and I need help.

I'm making a very simple calculator app in preparation for a much more complicated calculator app that I will be refactoring/porting from a Python shell. The basic idea for the simply calculator is this: I have 4 pages on 1 app. They contain an add, subtract, multiply, and divide function respectively. I have a working side menu (using bootstrap and some copied code) to navigate between the pages. Each page has a form on it with two charfields and a submit button, and should return the result of the operation associated with the page. Right now I'm just focusing on the 'add' page since the others will just be a matter of copying.

Currently, I think I have a working form for getting two inputs, but I have no idea how to then get those two inputs as variables I can add to get the result.

Here's my code so far:

views.py

from __future__ import unicode_literals

from django.http import HttpResponse

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from .forms import AddForm

@csrf_exempt
def web_adder(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = AddForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required

            # redirect to a new URL:
            return web_adder_out(form)

    # if a GET (or any other method) we'll create a blank form
    else:
        form = AddForm()

    return render(request, 'addercontent.html', {'form': form})

def web_adder_out(request, form):
    return render(request, 'addercontentout.html', {'content':[form.addend0 + form.addend1]})

#haven't gotten to this stuff yet
def web_subtracter(request):
    return render(request, 'subtractercontent.html')

def web_multiplier(request):
    return render(request, 'multipliercontent.html')

def web_divider(request):
    return render(request, 'dividercontent.html')

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.web_adder, name='web_adder'),
    url(r'^web_sub$', views.web_subtracter, name='web_subtracter'),
    url(r'^web_mul$', views.web_multiplier, name='web_multiplier'),
    url(r'^web_div$', views.web_divider, name='web_divider'),
]

forms.py

from django import forms

class AddForm(forms.Form):
    addend0 = forms.CharField(label='first addend', max_length=100)
    addend1 = forms.CharField(label='second addend', max_length=100)

addercontent.html

{% extends "header.html" %}
{% block content %}
<p>This is a web adder</p>
<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
{% endblock %}

addercontentout.html

{% extends "header.html" %}
{% block content %}
    {% for c in content%}
       <p>Result: {{c}}</p>
    {% endfor %}
{% endblock %}

(I know I'm not showing a lot of html files but they shouldn't matter)

In the above code I know the form in web_adder is different from the form in web_adder_out, that's where I'm at a loss. Although I wouldn't be surprised to see other mistakes.

I'm using Python 2.7. Thanks in advance.

Upvotes: 1

Views: 821

Answers (1)

N. Ivanov
N. Ivanov

Reputation: 1823

You can process each individual field in the form like so:

form.cleaned_data['field_name']

this gets you the actual value of the field.

Example with your form would be:

form.cleaned_data['addend0']

which will give you the POST value of addend0 field.

Hope this helps!

Upvotes: 1

Related Questions