3 回答
TA贡献1963条经验 获得超6个赞
我发现对我有用:
views.py :
def input(request):
return render(request, 'input.html', {})
def multiply(request):
if request.method == 'POST':
data = request.POST.get("decimalfield")
twice = int(data) * 2
return render(request, 'multiply.html', {'twice':twice})
urls.py :
path('input', views.input, name='input'),
path('multiply', views.multiply, name='multiply'),
输入.html
<form method="POST" action="{% url 'multiply' %}">
{% csrf_token %}
<input type="text" name="decimalfield">
<button type="submit">Upload text</button>
</form>
乘法.html
<label>{{ twice }}</label>
TA贡献1817条经验 获得超6个赞
参数应该是字典。views.py:
def multiply(request):
if request.method == 'POST':
data = request.POST.get("decimalfield")
twice = data * 2
return render(request, 'multiply.html', {'twice':twice})
return render(request, 'input.html')
为什么你有两个指向一个视图的网址?第二个是什么?删除它
path('input', views.multiply, name='input'),
# path('multiply', views.multiply, name='multiply'),
创建乘法.html并渲染两次:
<p>{{ twice }}</p>
TA贡献1802条经验 获得超4个赞
好的,因为当你通过url(name = 'input')进入页面时,第一个方法是请求的,所以他们不会重新运行渲染,因为它在方法POST中,你现在可以编辑:
def multiply(request):
if request.method == 'POST':
data = request.POST.get("decimalfield")
twice = {'data':data*2 }
return render(request, 'multiply.html', twice)
return render(request, 'multiply.html')
添加回答
举报