我有这种情况:
点击html提交按钮,我调用 views.stream_response
激活 views.stream_response_generator
其中激活stream.py 并返回一个 StreamingHttpResponse ,我看到逐行数字第二个 m
在 / stream_response /
:
1
2
3
4
5
6
7
8 // eg我的默认最大值m
stream.py
从django.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> \\\
%x #prints浏览器
print(lista)#print on eclipse
return(x)
views.py
def stream_response(request)://统一三函数如建议
如果request.method =='POST':
form = InputNumeroForm(request.POST)
如果form.is_valid():
m =请求.POST.get('numb',8)
resp = StreamingHttpResponse(stream.streamx(m))
return resp
forms.py
from django.db import模型
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'),
...
首页/ provadata.html
; form id =streamformaction ={%url'stream_response'%}method =POST>
{%csrf_token%}
{{form}}
< input id =numbtype =number/>
< input type =submitvalue =查看id =streambutton/>
< / form>
如果我删除8,只使用 m = request.POST。 get('numb')
我获取:
所以,如果我尝试提交,只需要默认值8(和工作),但它不需要我的表单输入。什么是错误?
views.py
def stream_response(request):
form = InputNumeroForm()
如果request.method =='POST':
form = InputNumeroForm(data = request.POST)
如果form.is_valid():
#访问clean_data中的数据
m = form.cleaned_data ['numero']
print(My form html:%s%form)
print(My Number:%s%m)#watch你的命令行
print(m =,m)
resp = StreamingHttpResponse(stream.streamx(m))
return resp
#如果不提供模板中的表单:
return render(request,'homepage / provadata.html',{'form':form,})
forms.py
class InputNumeroForm .Form):
numero = models.IntegerField()
homepage / provadata.py
< form action =/ stream_response /method =POST>
{%csrf_token%}
{{form}}<! - 问题:不出现在html !!!!! - >
< input type =numbername =numero/> <! - 所以我写这个 - >
< input type =submitvalue =查看/>
< / form>
如果我输入例如7从键盘:
如果我写 m = request.POST.get('numero')
,在命令行我有:
...
我的表单html:
我的号码: 7
m = 7
...
while len(lista)< m:
TypeError:不顺序类型:int()< str()
编辑: ModelForm部分已删除,无需将数据保存到DB中,所以使用经典格式:
方法1:您的 forms.py 中的Django
from django import forms
pre>
class InputNumeroForm(forms.Form):
numero = forms.IntegerField()
在您的 views.py
from django.shortcuts import render
def stream_response(request):
form = InputNumeroForm()
if request.method =='POST':
form = InputNumeroForm(data = request.POST)
如果form.is_valid():
#访问clean_data中的数据
m = form.cleaned_data ['numero']
print我的数字%s%m #watch你的命令行
resp = StreamingHttpResponse(stream.streamx(m))
返回resp
#如果不是在模板中提供表单:
return render(request,'homepage / provadata .html',{
'form':form,
})
在您的模板中:
< form id =streamformaction ={%url'stream_response'%}method = POST >
{%csrf_token%}
{{form}}
< input type =submitvalue =查看id =streambutton/>
< / form>
要澄清一点,Django中有两种类型的表单:
这里您不需要将您的号码保存在数据库,因此您使用古典表单:
方法2:不使用Django窗体
非常简单
views.py
from django.shortcuts import render
def stream_response(request):
if request.method =='POST':
if request.POST.get( 'numero',False):
m = int(request.POST ['numero'])
打印我的号码%s%m #watch你的命令行
resp = StreamingHttpResponse(stream.streamx(m))
return resp
return render请求,homepage / provadata.html)
在您的模板中:
< form id =streamformaction ={%url'stream_response'%}method =POST>
{%csrf_token%}
< input type =numbername =numero/>
< input type =submitvalue =查看id =streambutton/>
< / form>
I HAD this situation:
Clicking on a html submit button, I call views.stream_response
which "activates" views.stream_response_generator
which "activates" stream.py and return a StreamingHttpResponse and I see a progressive number every second up to m
at /stream_response/
:
1
2
3
4
5
6
7
8 //e.g. 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 #prints on browser
print(lista) #print on eclipse
return (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'),
...
homepage/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 delete "8" and use only m = request.POST.get('numb')
I obtain:
So, if I try to submit, it takes only the default value 8 (and works) but it not takes my form input. What is it wrong?
views.py
def stream_response(request):
form = InputNumeroForm()
if request.method == 'POST':
form = InputNumeroForm(data=request.POST)
if form.is_valid():
#Accessing the data in cleaned_data
m = form.cleaned_data['numero']
print("My form html: %s" % form)
print ("My Number: %s" % m) #watch your command line
print("m = ", m)
resp = StreamingHttpResponse(stream.streamx(m))
return resp
#If not post provide the form here in the template :
return render(request, 'homepage/provadata.html', {'form': form,})
forms.py
class InputNumeroForm(forms.Form):
numero = models.IntegerField()
homepage/provadata.py
<form action="/stream_response/" method="POST">
{% csrf_token %}
{{form}} <!--issue: does not appear on the html !!!!!-->
<input type="number" name="numero" /> <!--so I write this-->
<input type="submit" value="to view" />
</form>
If I give as input e.g. 7 from keyboard:
If I write m = request.POST.get('numero')
, in command line I have:
...
My form html:
My Number: 7
m = 7
...
while len(lista) < m:
TypeError: unorderable types: int() < str()
解决方案 EDIT : ModelForm part removed, no need to save the data in DB so using classic form this way :
Method 1 : Classic Form without Model using Django
in your forms.py
from django import forms
class InputNumeroForm(forms.Form):
numero = forms.IntegerField()
in your views.py
from django.shortcuts import render
def stream_response(request):
form = InputNumeroForm()
if request.method == 'POST':
form = InputNumeroForm(data=request.POST)
if form.is_valid():
#Accessing the data in cleaned_data
m = form.cleaned_data['numero']
print "My Number %s" % m #watch your command line
resp = StreamingHttpResponse(stream.streamx(m))
return resp
#If not post provide the form here in the template :
return render(request, 'homepage/provadata.html', {
'form': form,
})
in your template :
<form id="streamform" action="{% url 'stream_response' %}" method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="to view" id="streambutton" />
</form>
To clarify a little there are two types of form in Django :
Here you don't need to save your number in your database so you use classical forms:https://docs.djangoproject.com/en/1.8/topics/forms/
Method 2 : Not using the Django FormFor very simple forms like your case :
in views.py
from django.shortcuts import render
def stream_response(request):
if request.method == 'POST':
if request.POST.get('numero', False):
m = int(request.POST['numero'])
print "My Number %s" % m #watch your command line
resp = StreamingHttpResponse(stream.streamx(m))
return resp
return render(request, 'homepage/provadata.html')
in your template :
<form id="streamform" action="{% url 'stream_response' %}" method="POST">
{% csrf_token %}
<input type="number" name="numero" />
<input type="submit" value="to view" id="streambutton" />
</form>
这篇关于Django:将值从模板传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!