【问题标题】:Exporting django variable to python file将 django 变量导出到 python 文件
【发布时间】:2019-04-25 01:28:45
【问题描述】:

大家好,提前感谢您的帮助。

我想在我的 home.html 中引入一个数字,将其保存为变量 'n' 并将其导出到 my_calculations.py 文件中。

      <form>
        <label for='number'>Number:</label>
        <input type='number' value='Save'>
        <button type="submit">submit</button>
      </form>

你能解释一下我该怎么做吗? 我到处寻找,但有很多关于如何将变量从 .py 发送到模板的信息,但反之则不然。

【问题讨论】:

  • 从字面上看,part 4 of the Django tutorial 谈论将表单数据从模板发送到视图。
  • 谢谢丹尼尔,现在我想弄清楚如何将 selected_choice 从 views.py... def vote() 导入到 my_file.py。
  • 什么是 my_file.py?
  • its one file Ive 创建在与视图、模型 ets 相同的级别(我未来的应用程序将需要它)在代码行下的视图中:selected_choice.save() 我添加了:global a, a = selected_choice, print(a) ...但是如何将“a”变量导入 my_file.py?
  • 但这并没有什么意义。 my_file 中的函数调用是什么?

标签: django python-3.x django-templates


【解决方案1】:

您只需要一个具有 post 方法的表单

<form method="POST" action="to_view( a url )">
    {% csrf_token %}
    <label for='number'>Number:</label>
    <input type='number' name="value">
    <button type="submit">submit</button>
</form

并且这个表单必须由视图示例处理:

def view(request):
    # you can print the variable by print request.POST["value"]
    if request.method == "POST":
        do_Something

这只是一个模型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-04
    • 2020-04-30
    • 2019-10-11
    • 1970-01-01
    • 2013-08-28
    • 2017-09-05
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多