First save the form after the user has input values using - `userform.save()`
Then get that value using - val = userform.CharField
Now send the `val` in the context and show it to user wherever you want
PS- `CharField` would be whatever varuable name you have used in form/ModelForm
On Sun, 9 Jun 2019, 02:39 Majid Rafiee, <rafiee11@gmail.com> wrote:
I am trying to create a form which asks user to input a value, do some calculations on it and show the results back to the user. Here is what I've done: I have created an app called QMarcum. Inside this app I have this views.py, forms.py and urls.py:
views.py
from django.shortcuts import render
from scipy.integrate import quad
import scipy, numpy, math
from scipy import integrate
from django.http import HttpResponse
from .forms import Calculate
def integrand(x):
Q_y = (1/(math.sqrt(2*math.pi)))*math.exp((-x**2)/2)
return Q_y
y = Calculate.y
ans, err = quad(integrand, float(y), math.inf)
print(ans)
return render(request, 'test.html', {'form': Q_y})
forms.py
from django import forms
class Calculate(forms.Form):
y = forms.FloatField()
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('form', views.integrand),
]
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Submit</button>
</form>
</body>
</html>
The form asks user to input a value (y) (y is printed on terminal successfully), however, print(ans) doesn't print the value AND also I don't know how to show the calculated result (ans) to user.
Thanks in advance
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b77e2ebd-a440-4eb5-bd9d-a6635edd56a8%40googlegroups.com.
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAC3mK7cZ_y_C2B9kY3rukQMAXT9LohEk1cgBzxg_zzAKihW9ng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment