【问题标题】:Call a script within a view PYTHON / DJANGO在视图 PYTHON / DJANGO 中调用脚本
【发布时间】:2019-12-17 00:17:59
【问题描述】:

我是 Python 和 Django 或一般代码的新手

我创建了一个使用 javascript 从 html 模板中获取价值的视图。收集到这些值后,我想运行一个考虑这些新值的脚本

@csrf_exempt
def register(request):
      type_list=request.POST.get('selected_type').split("&") #array collected from checkboxes
      from_date=request.POST.get('start_date').replace("&","") #date collected
      to_date=request.POST.get('end_date').replace("&","") #date collected
      import transaction.query #script I want to run
      profiling = transactionprofiling.objects.all() #database/model updated with the script
      tmpl = loader.get_template("transaction/index.html")
      cont = {'profiling': profiling}
      return HttpResponse(tmpl.render(cont)) #return the model in the template

定义不起作用,因为它没有保存值“type_list”“from_date”“to_date” 我做了一些研究,但没有任何结果

提前感谢您的帮助!

【问题讨论】:

  • 欢迎来到 SO。您实际上是在哪里保存对象? transaction.query 里面有什么?是否出现任何错误?
  • 事务分析被保存到模型中,其他的暂时只在变量上,我也尝试将它们保存到模型中,但似乎什么都没有保存。
  • code code filterdate.start_date=from_date filterdate.end_date=to_date filterdate().save()
  • 进入查询我用 SQL 查询更新了名为“transactionprofiling”的表/模型,当我在 query.py 中声明随机变量时,一切似乎都正常

标签: python django import python-import


【解决方案1】:

如果有人对我找到的答案感兴趣,我会创建新模型并将结果保存在模型中,然后使用这些模型而不是变量来运行脚本!干杯!

@csrf_exempt
def register(request):
      type_list=request.POST.get('selected_type').split("&")
      from_date=request.POST.get('start_date').replace("&","")
      to_date=request.POST.get('end_date').replace("&","")

      filtertype.objects.all().delete()

      n = 0
      for item in type_list:
            n = n + 1
            print(item)
            add = filtertype(id=n, transaction_type=item)
            add.save()

      filterdate.objects.all().delete()

      add = filterdate(id=3, start_date=from_date,end_date=to_date)
      add.save()

      print(type_list,from_date,to_date)

      import transaction.query

      profiling = transactionprofiling.objects.all()
      tmpl = loader.get_template("transaction/index.html")
      cont = {'profiling': profiling}
      return HttpResponse(tmpl.render(cont))

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多