Sanjay Ortiz
Sanjay Ortiz

Reputation: 75

Add two numbers in django

Edited :

I'm new to django and i need to add two numbers x and y .

The x and y are inputs from user.

Here is my views.py

from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render
from .forms import InputForm

def add(request):
    if request.method == 'POST':        # If the form has been submitted...
     form = InputForm(request.POST)     # A form bound to the POST data
     if form.is_valid():                # All validation rules pass
        cd = form.cleaned_data     # Process the data in form.cleaned_data
        input1 = cd['x']
        input2 = cd['y']
        output = input1 + input2
        return HttpResponseRedirect(request,'/thanks/')# Redirect to new url
   else:
        form = InputForm()   # An unbound form


   return render(request, 'scraper/base.html', {'form': form })     


def thanks(request,output):
return render(request, 'scraper/base.html', output)

Here is my forms.py

from django import forms

class InputForm(forms.Form):
     x = forms.IntegerField(label='Value of x')
     y = forms.IntegerField(label='Value of y')

Here is my urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.add, name='add'),
url(r'^/', views.thanks , name='thanks'),
]

here is output.html

<html>
  <head>
  <title>Thanks</title>
  </head>
  <body>
  <h1>Thanks</h1>
  <h1> Output = {{output}} </h1>
  </body>
  </html>  

User inputs the x and y values then click add button the output should be displayed in a new page like output.html so how to do that ?

I know there are some errors in views.py i'm learning django. Please point it out and tell me the correct way to implement this

I'm struggling for 5 hours.

Upvotes: 0

Views: 7357

Answers (2)

utkbansal
utkbansal

Reputation: 2817

You can use sessions to store the output and then retrieve it in the next view. The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis.

Edit your views.py as follows -

def add(request):
    if request.method == 'POST':        
        form = InputForm(request.POST)     
        if form.is_valid():                
            cd = form.cleaned_data     
            input1 = cd['x']
            input2 = cd['y']
            output = input1 + input2
            # Save the result in the session
            request.session['output'] = output
            return  HttpResponseRedirect('/thanks/')
    else:
        form = InputForm() 
    return render(request, 'scraper/base.html', {'form': form })  


def thanks(request):
    # Get the result from the session
    output = request.session.pop('output', None)
    return render(request, 'scraper/output.html', {'output':output})

And you urls.py should be -

urlpatterns = [
    url(r'^$', views.add, name='add'),
    url(r'^thanks/$', views.thanks , name='thanks'),
]

Upvotes: 2

Dhia
Dhia

Reputation: 10619

I think you are using the redirect incorrectly, you can't send parameter in HttpResponseRedirect, check the modification here and take look in the documentation for more explanation:

views.py

def add(request):
    if request.method == 'POST':        # If the form has been submitted...
     form = InputForm(request.POST)     # A form bound to the POST data
     if form.is_valid():                # All validation rules pass
        cd = form.cleaned_data     # Process the data in form.cleaned_data
        input1 = cd['x']
        input2 = cd['y']
        output = input1 + input2
        return HttpResponseRedirect('/thanks/{output}/'.format(output=output)) # Redirect to new url
   else:
        form = InputForm()   # An unbound form
   return render(request, 'scraper/base.html', {'form': form })  

urls.py

url(r'^thanks/(?P<output>\d+)/$', thanks),

Upvotes: 1

Related Questions