Monday, July 21, 2014

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

On 22/07/2014 1:07 PM, Jordan S wrote:
> 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.
>

I'm interested in molecular mass calculation mainly because of isotopic
variances. How do you account for them? They seem to vary somewhat
depending on the laboratory doing the work. I'm building an application
and have made an interface to collect molecular weight from NIH or
Chemspider. But I would prefer to calculate weights than go and get them.

On your Django question, I would be creating a database record for each
substance formula and calculate the molecular weight in models.py then
put the value in a field just prior to save(). Once you have the data
you can just redisplay the same page.

Otherwise, detect that you actually have a molecular weight and display
a different page.

Here is an excerpt from my own Substance model ...

formula = models.CharField(max_length=LARGE)

molecular_weight = models.DecimalField(null=True, blank=True,
max_digits=10,
decimal_places=4,
default=0.0000)

... and this is where I would do the calculation ...

def save(self, *args, **kwargs):
self.molecular_weight = CalcMolecularMass(self.formula)
super(Substance, self).save(*args, **kwargs)

Mike

> 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>
> <formaction="{% url 'MolecularMass:result' %}"method="POST">
> {% csrf_token %}
> <inputtype="text"name="formula"/>
> <inputtype="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
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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
> <https://groups.google.com/d/msgid/django-users/2feffc32-36f1-49c0-9561-ce9945ffe12d%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
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/53CDFAD8.1020007%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment