由于每个问题最好有一个问题,因此如果与问题的其他部分相似,请耐心等待另一个我的问题 https://stackoverflow.com/questions/29921085/django-passing-value-from-form-to-view-without-saving-into-database与同一项目相关。
情况:
我有一个 html 表单,可以在其中设置一个数字,提交后,它会被调用views.stream_response
将值传递给流.py它返回一个流式Http响应并出现“虚拟”空白浏览器页面(/流响应/)其中我可以看到每秒一个渐进的数字,直到m
:
1
2
3
..
m
流.py
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
print(lista[-1])
return (x)
- -更新 - -
views.py
def stream_response(request):
test = InputNumeroForm()
if request.method == 'POST':
test = InputNumeroForm(data=request.POST)
if test.is_valid():
m = test.cleaned_data['numero']
print (test)
print("m = ", m)
#resp = StreamingHttpResponse(stream_response_generator(m))
resp = StreamingHttpResponse(stream.streamx(m))
return resp
return render(request, 'homepage/provadata.html',{'user.username': request, 'test': test}, context_instance = RequestContext(request))
urls.py
...
url(r'^homepage/provadata/$', views.provadata),
url(r'^stream_response/$', views.stream_response, name='stream_response'),
...
主页/provadata.html
<form id="numero" action="/stream_response_bis/" method="POST">
{% csrf_token %}
{{test}}
<input type="submit" value="to view" />
</form>
//{{ris}}
我尝试做一个 render_to 响应以保持不变主页/provadata.html并查看渐进列表,但是流.py无法启动,我只能看到输入的数字m在命令行上。
我尝试过THIS https://stackoverflow.com/questions/15116881/django-streaminghttpresponse-into-a-template建议中views.py
def stream_response_generator(m):
ris = stream.streamx(m)
yield loader.get_template('homepage/provadata.html').render(Context({'ris': ris}))
(adding {{ris}}
到模板和
resp = StreamingHttpResponse(stream_response_generator(m))
in 流响应功能)
但我在模板上得到:
<generator object streamx at 0x0000000004BEB870>
在命令行上,它打印输入值,但不再将参数传递给stream.py。
So.. 我该如何解决这个问题?