【问题标题】:Use Subprocess in a form action of a template在模板的表单操作中使用子流程
【发布时间】:2019-02-06 12:50:18
【问题描述】:

在模板的表单动作中,有没有办法运行脚本?

     <form action="{% subprocess.check_call(['python', 'myFile.py']) %}" method="post">
<input class="btn_ok" type="submit" value="Validate" />

       </form>

【问题讨论】:

  • HTML 表单的“action”属性是表单应该提交到的 url(如果与当前 url 不同)。鉴于您的问题是多么荒谬(对于知道这是如何工作的人),我强烈建议您了解 HTTP 协议的工作原理以及前端和后端代码之间的区别。

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


【解决方案1】:

不,这没有任何意义。

该操作必须是一个带有您希望该操作发生的 URL 的字符串。

您应该做的是创建一个视图来执行您在myFile.py 中尝试执行的任何处理,或者让视图运行子进程并运行该文件,如果您确实必须这样做的话。

【讨论】:

  • 在操作表单中我不想调用 url。可以举个例子吗?
  • 没有例子。表单必须调用一个 URL。没有别的办法了。
  • 您阅读过这里的文档吗? docs.djangoproject.com/en/2.1/topics/forms
【解决方案2】:

我做到了。我没有使用子进程。 我之所以问我发布它的问题是因为我想在同一个索引页面中了解如何在我的应用程序中获取会议代码而不在另一个模板中重定向。

在我的应用程序的 urls.py 中,我提到了路径:

urlpatterns = [
    path('', views.index),
]

在我的模板中,我有一个由查询集参加的会议列表;通过使用块 {% for item in m_list_key %}。我在迭代“for”中创建了一个小表单(带有提交按钮):

<form action="/meetingPage/" method="post">
   {% csrf_token %}
   <input type="hidden" value="{{ item.codeMeeting }}" id="code_meet" name="codeMeeting_name" />
   <input class="btnParticipate" type="submit" value="Participate" />
</form>

在我的应用meetingPage的views.py中,索引函数为:

 def index(request):
        meetings = Meeting.objects.all()
        if request.method == 'POST':
            the_code_meeting = request.POST['codeMeeting_name']
            return render(request, 'meetingPage/index.html',{'code_meeting_key': the_code_meeting,'m_list_key': meetings }) 
        return render(request, 'meetingPage/index.html')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多