I had this situation:
By clicking on the html submit button, I call views.stream_response , which “activates” views.stream_response_generator , which “activates” stream.py and returns StreamingHttpResponse, and I see a progressive number every second up to m in /stream_response/
1 2 3 4 5 6 7 8 //eg my default max value for m
stream.py
from django.template import Context, Template import time def streamx(m): lista = [] x=0 while len(lista) < m: x = x + 1 time.sleep(1) lista.append(x) yield "<div>%s</div>\n" % x
views.py
def stream_response(request): // unified the three functions as suggested if request.method == 'POST': form = InputNumeroForm(request.POST) if form.is_valid(): m = request.POST.get('numb', 8) resp = StreamingHttpResponse(stream.streamx(m)) return resp
forms.py
from django.db import models from django import forms from django.forms import ModelForm class InputNumero(models.Model): m = models.IntegerField() class InputNumeroForm(forms.Form): class Meta: models = InputNumero fields = ('m',)
urls.py
... url(r'^homepage/provadata/$', views.provadata), url(r'^stream_response/$', views.stream_response, name='stream_response'), ...
Home /provadata.html
<form id="streamform" action="{% url 'stream_response' %}" method="POST"> {% csrf_token %} {{form}} <input id="numb" type="number" /> <input type="submit" value="to view" id="streambutton" /> </form>
If I remove "8" and use only m = request.POST.get('numb') , I get:
ValueError at / stream_response / View homepage.views.stream_response does not return an HttpResponse object. Instead, None was specified instead.
So, if I try to submit, it only accepts the default value of 8 (and it works), but it does not accept my input form. What is this wrong?
→ UPDATE: with @Tanguy Serrat suggestions:
views.py
def stream_response(request): form = InputNumeroForm() if request.method == 'POST': form = InputNumeroForm(data=request.POST) if form.is_valid():
forms.py
class InputNumeroForm(forms.Form): numero = models.IntegerField()
Home /provadata.py
<form action="/stream_response/" method="POST"> {% csrf_token %} {{form}} <input type="number" name="numero" /> <input type="submit" value="to view" /> </form>
If I give input, for example. 7 from the keyboard:
KeyError at / stream_response /
'Numero' 
WHILE
If I write m = request.POST.get('numero') , on the command line I have:
... My form html: My Number: 7 m = 7 ... while len(lista) < m: TypeError: unorderable types: int() < str()