【问题标题】:I want to see the value when i press particular number button in django我想在 django 中按下特定数字按钮时查看值
【发布时间】:2019-12-24 02:38:53
【问题描述】:

我有一个小型 Django 计算器应用程序。我想在单击任何数字按钮时在文本框中显示值

我的 HTML 代码

 <form action ="buttonClicked" method="POST">
        {% csrf_token %}
      <div class="calculator">

        <input type="text" class="calculator-screen" value="0" disabled name = "text-box"/>

        <div class="calculator-keys">

          <button type="button" class="operator" value="+">+</button>
          <button type="button" class="operator" value="-">-</button>
          <button type="button" class="operator" value="*">&times;</button>
          <button type="button" class="operator" value="/">&divide;</button>

          <button type="button" value="7">7</button>
          <button type="button" value="8">8</button>
          <button type="button" value="9">9</button>


          <button type="button" value="4">4</button>
          <button type="button" value="5">5</button>
          <button type="button" value="6">6</button>


          <button type="submit" value="1" name="btn1">1</button>
          <button type="button" value="2">2</button>
          <button type="button" value="3">3</button>


          <button type="button" value="0">0</button>
          <button type="button" class="decimal" value=".">.</button>
          <button type="button" class="all-clear" value="all-clear">AC</button>

          <button type="button" class="equal-sign" value="=">=</button>

        </div>
      </div>
    </form>
    {% endblock %}

我的视图.py

from django.shortcuts import render
from django.contrib import messages

    def calc(request):
        if request.method == 'POST':
           #here i want to take the button value and display it on text box
        return render(request, 'Calculator.html')

Calc

如果我按下按钮 1,那么它应该在文本框中显示 1... 我该怎么做?

解决了我的问题

查看.py

def calc(request):
        if(request.POST.get('btn1')):

                print('Button clicked')
                return render(request, 'Calculator.html',{'val':100})
        else:
                return render(request, 'Calculator.html',{'val':0})

【问题讨论】:

  • 使用J-query设置输入值
  • 我只想用python来做。谢谢

标签: python html django view


【解决方案1】:

我不确定您是否可以看到该值,但如果您为按钮命名,您可以检查它是否在 POST 中。例如:

    <div class="calculator-keys">

      <button type="button" class="operator" value="+" name="btn_plus">+</button>
      <button type="button" class="operator" value="-" name="btn_minus">-</button>
      <button type="button" class="operator" value="*" name="btn_times">&times;</button>
      <button type="button" class="operator" value="/" name="btn_divide">&divide;</button>
    </div>

然后在视图内:

if 'btn_plus' in request.POST: ...

【讨论】:

  • 我可以检查你给出的逻辑。但我想在文本框中看到该值。我不知道如何将值传递给文本框
猜你喜欢
  • 2022-10-05
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多