Monday, July 21, 2014

How do I make my form process data and output in another page?

Hi, I'm trying to make a molecular mass calculator webpage, where a user inputs a chemical formula and gets the mass as a result. I've made this work in python using raw_input(), but now I'd need to use a form with django. 

I have the html form set up, but when I click "submit", the page just shows itself again.

What I want to happen is:

1. User inputs formula in form on index.html(ex. H2)
2. Form data is processed using the code that I put in the result view
3. Result is outputted in result.html

However, I'm not sure how to do this, as I've looked through the tutorial and form docs.

Here is my views.py:
  1. from django.shortcuts import render
  2. from django.db import connection
  3. from django.http import HttpResponseRedirect
  4. from django.core.urlresolvers import reverse
  5. import sys, re
  6.  
  7. # Create your views here.
  8. def index(request):
  9.         if request.method == "POST":
  10.                 return HttpResponseRedirect(reverse('MolecularMass:result'))
  11.         else:
  12.                 return render(request, 'MolecularMass/index.html')
  13. def result(request):
  14.         c = connections['default'].cursor()
  15.  
  16.         def FindEndParentheses(separated):
  17.                 count = 0
  18.                 for index, val in enumerate(separated):
  19.                         if val == ')':
  20.                                 count -= 1
  21.                                 if count == 0:
  22.                                         return index
  23.                         elif val == '(':
  24.                                 count += 1
  25.                 raise NameError('Please close the parentheses')
  26.         def GetAtomicMass(val):
  27.                 c.execute("SELECT Atomic_Weight FROM Elements WHERE Atomic_Numer=%s" % val)
  28.                 return c.fetchone[0]
  29.         def Parser(separated, total):
  30.                 if len(separated) == 0:
  31.                         return sum(total)
  32.                 val = separated[0]
  33.                 if val == '(':
  34.                         end = FindEndParentheses(separated)
  35.                         total.append(Parser(separated[1:end], []))
  36.                         return Parser(separated[end + 1:], total)
  37.                 elif val.isdigit():
  38.                         total[-1] *= int(val)
  39.                 else:
  40.                         total.append(GetAtomicMass(val))
  41.                 return Parser(separated[1:], total)
  42.         def CalcMolecularMass(formula):
  43.                 separated = re.findall(r'[A-Z][a-z]*|\d+|\(|\)', formula)
  44.                 return('The molecular mass of {} is {:.3f}\n'.format(formula, Parser(separated, [])))
  45.         content = CalcMolecularMass(formula)
  46.         return render(request, 'MolecularMass/result.html', content)
My index.html:

   
<h1>Enter a chemical formula to get its molecular mass</h1>
   
<form action="{% url 'MolecularMass:result' %}" method="POST">
    {% csrf_token %}
       
<input type="text" name="formula"/>
   
<input type="submit" value="Submit"/>
   
</form>


My result.html:

   
{{ content }}


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2feffc32-36f1-49c0-9561-ce9945ffe12d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment